2. 第三方连接池

网友投稿 782 2022-09-10

2. 第三方连接池

2. 第三方连接池

2. 第三方连接池

前言

在前面的章节中,我们虽然写了下自定义连接池,但是那只是为了更好理解连接池原理而已。在真正的工作中,我们使用最多的还是第三方连接池。

常用连接池

常见的第三方连接池如下:

C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。C3P0是异步操作的,所以一些操作时间过长的JDBC通过其它的辅助线程完成。目前使用它的开源项目有Hibernate,Spring等。C3P0有自动回收空闲连接功能阿里巴巴-德鲁伊druid连接池:Druid是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池、插件框架和SQL解析器组成。该项目主要是为了扩展JDBC的一些限制,可以让程序员实现一些特殊的需求。DBCP(DataBase Connection Pool)数据库连接池,是Apache上的一个Java连接池项目,也是Tomcat使用的连接池组件。dbcp没有自动回收空闲连接的功能。

下面我们专门来讲解一下 C3P0 和 德鲁伊druid连接池 即可。

C3P0 的基本使用

1.  介绍

C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目有Hibernate、Spring等。

在常见的开发中,一般都是- jar 包导入依赖,那么在这里呢。我采用 Maven - jar 包依赖,但是还是要-一下 C3P0 的项目,因为还需要看看里面的说明文档。

2.说明文档的-

​​zip 包如下:

image-20201027220821109

解压文件,如下:

image-20201027221118721

C3P0 的 jar 包 :

image-20201027231850014

那么下面我在项目中使用 maven 来设置 C3P0 的依赖。

3.使用 maven 设置 C3P0 的依赖

访问 搜索 c3p0 依赖

image-20210126080304523

image-20210126080321279

com.mchange c3p0 0.9.5.5

3.2 搜索 mchange-commons 依赖

image-20210126080603753

image-20210126080717375

com.mchange mchange-commons-java 0.2.20

3.3 在项目的 pom.xml 配置依赖

image-20210126080917398

com.mchange c3p0 0.9.5.5 com.mchange mchange-commons-java 0.2.20 mysql mysql-connector-java 5.1.49 junit junit 4.12 test

4. 查看开发文档,快速入门示例

示例代码以及配置文件:

image-20201027221701487

查看开发文档:

image-20201027221827398

image-20201027221850254

image-20201027221933416

5. 实现方式一:根据快速入门示例,实现数据库连接池

//方式一:@Testpublic void test01() throws Exception { //1.获取c3p0数据库连接池 ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false" ); cpds.setUser("root"); // 设置访问的用户 cpds.setPassword("L******************0"); // 设置访问的密码 //通过设置相关的参数,对数据库连接池进行管理: //设置初始时数据库连接池中的连接数 cpds.setInitialPoolSize(10); //获取连接池的数据库连接 Connection conn = cpds.getConnection(); System.out.println(conn); //销毁c3p0数据库连接池【一般不操作】 //cpds.close();}

测试执行如下:

image-20210126081538484

好了,到了这里。我们就已经知道的第一种创建的方式。当然,还有第二种方式,上面我们的参数都是固定写在代码中,应该将其抽离出来,写在配置文件中。

6. 查看开发文档,使用配置文件的方式来创建数据库连接池

image-20201027233628769

image-20201027233851275

7. 实现方式二:使用配置文件的方式,创建连接

1. 在项目的目录下,创建配置文件:​​c3p0-config.xml​​

image-20210126083450247

com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/test root *********** 5 10 10 100 50 2

2.创建数据库连接池

//方式二:使用配置文件@Testpublic void testGetConnection1() throws SQLException { ComboPooledDataSource cpds = new ComboPooledDataSource("hellc3p0"); Connection conn = cpds.getConnection(); System.out.println(conn);}

测试执行如下:

image-20210126083709840

8. 使用c3p0改写工具类

/** * @author Aron.li * @date 2020/10/27 23:51 */public class JDBCUtils { /** * * @Description 使用C3P0的数据库连接池技术 * @author shkstart * @date 下午3:01:25 * @return * @throws SQLException */ //数据库连接池只需提供一个即可。 private static ComboPooledDataSource cpds = new ComboPooledDataSource("hellc3p0"); public static Connection getConnection() throws SQLException{ Connection conn = cpds.getConnection(); return conn; } }

在这里注意使用 static 定义连接池,因为连接池只需要有一个即可。

DRUID

1.  DRUID介绍

Druid是阿里巴巴开发的号称为监控而生的数据库连接池,Druid是国内目前最好的数据库连接池。在功能、性能、扩展性方面,都超过其他数据库连接池。Druid已经在阿里巴巴部署了超过600个应用,经过很久生产环境大规模部署的严苛考验。如:一年一度的双十一活动,每年春运的抢火车票

Druid的-:使用 maven 设置 druid 的依赖

2.1 搜索依赖

访问 搜索 druid

image-20210126110446558

image-20210126110515728

image-20210126110528650

com.alibaba druid 1.2.4

2.2 在项目的 pom.xml 配置依赖

image-20210126110713396

com.alibaba druid 1.2.4 mysql mysql-connector-java 5.1.49 junit junit 4.12 test

3. DRUID的使用

3.1通过硬编码方式【了解】

步骤:

在 Maven 设置 DRUID jar 包创建Druid连接池对象, 配置4个基本参数从Druid连接池对象获得Connection

实现:

image-20210126111236347

@Test public void test01() throws SQLException { //1. 创建DataSource DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver");//设置驱动 dataSource.setUrl("jdbc:mysql://localhost:3306/test");//设置数据库路径 dataSource.setUsername("root");//设置用户名 dataSource.setPassword("L****************0");//设置密码 dataSource.setInitialSize(5);//设置初始化连接的数量 //2. 从数据源里面获得Connection Connection connection = dataSource.getConnection(); System.out.println(connection); }

3.2  通过配置文件方式【重点】

步骤:

在 Maven 设置 DRUID jar 包拷贝配置文件到src目录根据配置文件创建Druid连接池对象从Druid连接池对象获得Connection

实现:

创建druid.properties, 放在resources目录下

image-20210126111742287

url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=falseusername=rootpassword=Li****************0driverClassName=com.mysql.jdbc.DriverinitialSize=10maxActive=10

编写Java代码

image-20210126111817029

@Test public void test02() throws Exception { //0 根据druid.properties创建配置文件对象 Properties properties = new Properties(); // 关联druid.properties文件 InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties"); properties.load(is); //1. 创建DataSource DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); //2. 从数据源(连接池)获得连接对象 Connection connection = dataSource.getConnection(); System.out.println(connection); }

4. Druid工具类

为了方便调用,我们还可以将连接池的初始化封装一下,设置为静态属性即可。

public class DruidUtil { private static DataSource dataSource; static { try { //1. 创建Properties对象 Properties properties = new Properties(); //2. 将配置文件转换成字节输入流 InputStream is = DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties"); //3. 使用properties对象加载is properties.load(is); //druid底层是使用的工厂设计模式,去加载配置文件,创建DruidDataSource对象 dataSource = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } public static DataSource getDataSource(){ return dataSource; }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:22.Go面向对象-接口
下一篇:不能访问github怎么办?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~