赞
踩
目录
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,获取速度快,同时可以大幅度提高数据库访问速度。
1、获取连接的方式一:
- //使用C3P0数据库连接池的方式,获取数据库的连接:不推荐
- public static Connection getConnection1() throws Exception{
- ComboPooledDataSource cpds = new ComboPooledDataSource();
- cpds.setDriverClass("com.mysql.jdbc.Driver");
- cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
- cpds.setUser("root");
- cpds.setPassword("abc123");
-
- // cpds.setMaxPoolSize(100);
-
- Connection conn = cpds.getConnection();
- return conn;
- }
2、获取连接的方式二:
- //使用C3P0数据库连接池的配置文件方式,获取数据库的连接:推荐
- private static DataSource cpds = new ComboPooledDataSource("helloc3p0");
- public static Connection getConnection2() throws SQLException{
- Connection conn = cpds.getConnection();
- return conn;
- }
其中,src下的配置文件为:(c3p0-config.xml
)
- <?xml version="1.0" encoding="UTF-8"?>
- <c3p0-config>
- <named-config name="helloc3p0">
- <!-- 获取连接的4个基本信息 -->
- <property name="user">root</property>
- <property name="password">abc123</property>
- <property name="jdbcUrl">jdbc:mysql:///test</property>
- <property name="driverClass">com.mysql.jdbc.Driver</property>
-
- <!-- 涉及到数据库连接池的管理的相关属性的设置 -->
- <!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
- <property name="acquireIncrement">5</property>
- <!-- 初始化数据库连接池时连接的数量 -->
- <property name="initialPoolSize">5</property>
- <!-- 数据库连接池中的最小的数据库连接数 -->
- <property name="minPoolSize">5</property>
- <!-- 数据库连接池中的最大的数据库连接数 -->
- <property name="maxPoolSize">10</property>
- <!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->
- <property name="maxStatements">20</property>
- <!-- 每个连接同时可以使用的 Statement 对象的个数 -->
- <property name="maxStatementsPerConnection">5</property>
-
- </named-config>
- </c3p0-config>

1、DBCP 是 Apache 软件基金组织下的开源连接池实现,该连接池依赖该组织下的另一个开源系统:Common-pool。
如需使用该连接池实现,应在系统中增加如下两个 jar 文件:
2、Tomcat 的连接池正是采用该连接池来实现的。该数据库连接池既可以与应用服务器整合使用,也可由应用程序独立使用。
3、数据源和数据库连接不同,数据源无需创建多个,它是产生数据库连接的工厂,因此整个应用只需要一个数据源即可。
4、当数据库访问结束后,程序还是像以前一样关闭数据库连接:conn.close();但上面的代码并没有关闭数据库的物理连接,它仅仅把数据库连接释放,归还给了数据库连接池。
配置属性说明:
(1)获取连接方式一:
- public static Connection getConnection3() throws Exception {
- BasicDataSource source = new BasicDataSource();
-
- source.setDriverClassName("com.mysql.jdbc.Driver");
- source.setUrl("jdbc:mysql:///test");
- source.setUsername("root");
- source.setPassword("abc123");
-
- //
- source.setInitialSize(10);
-
- Connection conn = source.getConnection();
- return conn;
- }
(2)获取连接方式二:
- //使用dbcp数据库连接池的配置文件方式,获取数据库的连接:推荐
- private static DataSource source = null;
- static{
- try {
- Properties pros = new Properties();
-
- InputStream is = DBCPTest.class.getClassLoader().getResourceAsStream("dbcp.properties");
-
- pros.load(is);
- //根据提供的BasicDataSourceFactory创建对应的DataSource对象
- source = BasicDataSourceFactory.createDataSource(pros);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
- public static Connection getConnection4() throws Exception {
-
- Connection conn = source.getConnection();
-
- return conn;
- }

其中,src下的配置文件为:(dbcp.properties
)
- driverClassName=com.mysql.jdbc.Driver
- url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true&useServerPrepStmts=false
- username=root
- password=abc123
-
- initialSize=10
- #...
Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、Proxool等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池,可以说是目前最好的连接池之一。
- import java.sql.Connection;
- import java.util.Properties;
- import javax.sql.DataSource;
- import com.alibaba.druid.pool.DruidDataSourceFactory;
-
- public class TestDruid {
- public static void main(String[] args) throws Exception {
- Properties pro = new Properties(); pro.load(TestDruid.class.getClassLoader().getResourceAsStream("druid.properties"));
- DataSource ds = DruidDataSourceFactory.createDataSource(pro);
- Connection conn = ds.getConnection();
- System.out.println(conn);
- }
- }
其中,src下的配置文件为:(druid.properties
)
- url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
- username=root
- password=123456
- driverClassName=com.mysql.jdbc.Driver
-
- initialSize=10
- maxActive=20
- maxWait=1000
- filters=wall
详细参数配置:
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。