当前位置:   article > 正文

JDBC之多种开源数据库连接池介绍_开源 数据库连接池

开源 数据库连接池

目录

一、多种开源的数据库连接池

二、C3P0数据库连接池

三、DBCP数据库连接池

四、 Druid(德鲁伊)数据库连接池


一、多种开源的数据库连接池

1、JDBC 的数据库连接池使用 javax.sql.DataSource 来表示,DataSource 只是一个接口,该接口通常由服务器(Weblogic, WebSphere, Tomcat)提供实现,也有一些开源组织提供实现:

(1)DBCP 是Apache提供的数据库连接池。tomcat服务器自带dbcp数据库连接池。速度相对C3P0较快,但因自身存在BUG,Hibernate3已不再提供支持。

(2)C3P0 是一个开源组织提供的一个数据库连接池,速度相对较慢,稳定性还可以,hibernate官方推荐使用。

(3)Proxool 是sourceforge下的一个开源项目数据库连接池,有监控连接池状态的功能,稳定性较c3p0差一点.

(4)BoneCP 是一个开源组织提供的数据库连接池,速度快。

(5)Druid 是阿里提供的数据库连接池,据说是集DBCP 、C3P0 、Proxool优点于一身的数据库连接池,但是速度不确定是否有BoneCP快。

2、DataSource 通常被称为数据源,它包含连接池和连接池管理两个部分,习惯上也经常把 DataSource 称为连接池。

3、DataSource用来取代DriverManager来获取Connection,获取速度快,同时可以大幅度提高数据库访问速度。

二、C3P0数据库连接池

1、获取连接的方式一:

  1. //使用C3P0数据库连接池的方式,获取数据库的连接:不推荐
  2. public static Connection getConnection1() throws Exception{
  3. ComboPooledDataSource cpds = new ComboPooledDataSource();
  4. cpds.setDriverClass("com.mysql.jdbc.Driver");
  5. cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
  6. cpds.setUser("root");
  7. cpds.setPassword("abc123");
  8. // cpds.setMaxPoolSize(100);
  9. Connection conn = cpds.getConnection();
  10. return conn;
  11. }

2、获取连接的方式二:

  1. //使用C3P0数据库连接池的配置文件方式,获取数据库的连接:推荐
  2. private static DataSource cpds = new ComboPooledDataSource("helloc3p0");
  3. public static Connection getConnection2() throws SQLException{
  4. Connection conn = cpds.getConnection();
  5. return conn;
  6. }

其中,src下的配置文件为:(c3p0-config.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <c3p0-config>
  3. <named-config name="helloc3p0">
  4. <!-- 获取连接的4个基本信息 -->
  5. <property name="user">root</property>
  6. <property name="password">abc123</property>
  7. <property name="jdbcUrl">jdbc:mysql:///test</property>
  8. <property name="driverClass">com.mysql.jdbc.Driver</property>
  9. <!-- 涉及到数据库连接池的管理的相关属性的设置 -->
  10. <!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
  11. <property name="acquireIncrement">5</property>
  12. <!-- 初始化数据库连接池时连接的数量 -->
  13. <property name="initialPoolSize">5</property>
  14. <!-- 数据库连接池中的最小的数据库连接数 -->
  15. <property name="minPoolSize">5</property>
  16. <!-- 数据库连接池中的最大的数据库连接数 -->
  17. <property name="maxPoolSize">10</property>
  18. <!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->
  19. <property name="maxStatements">20</property>
  20. <!-- 每个连接同时可以使用的 Statement 对象的个数 -->
  21. <property name="maxStatementsPerConnection">5</property>
  22. </named-config>
  23. </c3p0-config>

三、DBCP数据库连接池

1、DBCP 是 Apache 软件基金组织下的开源连接池实现,该连接池依赖该组织下的另一个开源系统:Common-pool。

如需使用该连接池实现,应在系统中增加如下两个 jar 文件:

  • Commons-dbcp.jar:连接池的实现
  • Commons-pool.jar:连接池实现的依赖库

2、Tomcat 的连接池正是采用该连接池来实现的。该数据库连接池既可以与应用服务器整合使用,也可由应用程序独立使用。

3、数据源和数据库连接不同,数据源无需创建多个,它是产生数据库连接的工厂,因此整个应用只需要一个数据源即可。

4、当数据库访问结束后,程序还是像以前一样关闭数据库连接:conn.close();但上面的代码并没有关闭数据库的物理连接,它仅仅把数据库连接释放,归还给了数据库连接池。

配置属性说明:


 

 (1)获取连接方式一:

  1. public static Connection getConnection3() throws Exception {
  2. BasicDataSource source = new BasicDataSource();
  3. source.setDriverClassName("com.mysql.jdbc.Driver");
  4. source.setUrl("jdbc:mysql:///test");
  5. source.setUsername("root");
  6. source.setPassword("abc123");
  7. //
  8. source.setInitialSize(10);
  9. Connection conn = source.getConnection();
  10. return conn;
  11. }

(2)获取连接方式二:

  1. //使用dbcp数据库连接池的配置文件方式,获取数据库的连接:推荐
  2. private static DataSource source = null;
  3. static{
  4. try {
  5. Properties pros = new Properties();
  6. InputStream is = DBCPTest.class.getClassLoader().getResourceAsStream("dbcp.properties");
  7. pros.load(is);
  8. //根据提供的BasicDataSourceFactory创建对应的DataSource对象
  9. source = BasicDataSourceFactory.createDataSource(pros);
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. public static Connection getConnection4() throws Exception {
  15. Connection conn = source.getConnection();
  16. return conn;
  17. }

其中,src下的配置文件为:(dbcp.properties

  1. driverClassName=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true&useServerPrepStmts=false
  3. username=root
  4. password=abc123
  5. initialSize=10
  6. #...

四、 Druid(德鲁伊)数据库连接池

Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、Proxool等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池,可以说是目前最好的连接池之一。

  1. import java.sql.Connection;
  2. import java.util.Properties;
  3. import javax.sql.DataSource;
  4. import com.alibaba.druid.pool.DruidDataSourceFactory;
  5. public class TestDruid {
  6. public static void main(String[] args) throws Exception {
  7. Properties pro = new Properties(); pro.load(TestDruid.class.getClassLoader().getResourceAsStream("druid.properties"));
  8. DataSource ds = DruidDataSourceFactory.createDataSource(pro);
  9. Connection conn = ds.getConnection();
  10. System.out.println(conn);
  11. }
  12. }

其中,src下的配置文件为:(druid.properties

  1. url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
  2. username=root
  3. password=123456
  4. driverClassName=com.mysql.jdbc.Driver
  5. initialSize=10
  6. maxActive=20
  7. maxWait=1000
  8. filters=wall

详细参数配置:

 

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/blog/article/detail/57225
推荐阅读
相关标签
  

闽ICP备14008679号