在使用开发基于数据库的web程序时,传统的模式基本是按以下步骤:
在主程序(如servlet、beans)中建立数据库连接。
进行sql操作
断开数据库连接。
这种模式开发,存在的问题:
数据库的连接资源并没有得到很好的重复利用. 对于每一次数据库连接,使用完后都得断开。
这种开发不能控制被创建的连接对象数,系统资源会被毫无顾及的分配出去,如连接过多,也可能导致内存泄漏,服务器崩溃。
数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”。
数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是重新建立一个。
资源重用; 更快的系统反应速度; 新的资源分配手段; 统一的连接管理,避免数据库连接泄露.
JDBC 的数据库连接池使用 javax.sql.DataSource 来表示,DataSource 只是一个接口,该接口通常由服务器(Weblogic, WebSphere, Tomcat)提供实现,也有一些开源组织提供实现:
DBCP 数据库连接池
C3P0 数据库连接池
DataSource 通常被称为数据源,它包含连接池和连接池管理两个部分,习惯上也经常把 DataSource 称为连接池
Tomcat 的连接池正是采用该连接池来实现的。该数据库连接池既可以与应用服务器整合使用,也可由应用程序独立使用。
使用 DBCP 数据库连接池
* 1. 加入 jar 包(2 个jar 包). Commons-dbcp.jar:连接池的实现; Commons-pool.jar:连接池实现的依赖库
* 2. 创建数据库连接池
* 3. 为数据源实例指定必须的属性
* 4. 从数据源中获取数据库连接
public void testDBCP() throws SQLException{
final BasicDataSource dataSource = new BasicDataSource();
//2. 为数据源实例指定必须的属性
dataSource.setUsername("root");
dataSource.setPassword("1230");
dataSource.setUrl("jdbc:mysql:///atguigu");
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//3. 指定数据源的一些可选的属性.
//1). 指定数据库连接池中初始化连接数的个数
dataSource.setInitialSize(5);
//2). 指定最大的连接数: 同一时刻可以同时向数据库申请的连接数
dataSource.setMaxActive(5);
//3). 指定小连接数: 在数据库连接池中保存的最少的空闲连接的数量
dataSource.setMinIdle(2);
//4).等待数据库连接池分配连接的最长时间. 单位为毫秒. 超出该时间将抛出异常.
dataSource.setMaxWait(1000 * 5);
//4. 从数据源中获取数据库连接
Connection connection = dataSource.getConnection();
System.out.println(connection.getClass());
}
/**
* 1. 加载 dbcp 的 properties 配置文件: 配置文件中的键需要来自 BasicDataSource
* 的属性.
* 2. 调用 BasicDataSourceFactory 的 createDataSource 方法创建 DataSource
* 实例
* 3. 从 DataSource 实例中获取数据库连接.
*/
@Test
public void testDBCPWithDataSourceFactory() throws Exception{
Properties properties = new Properties();
InputStream inStream = JDBCTest.class.getClassLoader()
.getResourceAsStream("dbcp.properties");
properties.load(inStream);
DataSource dataSource =
BasicDataSourceFactory.createDataSource(properties);
System.out.println(dataSource.getConnection());
}
dbcp.properties
username=root password=1230 driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql:///atguigu initialSize=10 maxActive=50 minIdle=5 maxWait=5000
当数据库访问结束后,程序还是像以前一样关闭数据库连接:conn.close(); 但上面的代码并没有关闭数据库的物理连接,它仅仅把数据库连接释放,归还给了数据库连接池。
@Test
public void testC3P0() throws Exception{
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl( "jdbc:mysql:///atguigu" );
cpds.setUser("root");
cpds.setPassword("1230");
System.out.println(cpds.getConnection());
}
/**
* 1. 创建 c3p0-config.xml 文件,
* 参考帮助文档中 Appendix B: Configuation Files 的内容
* 2. 创建 ComboPooledDataSource 实例;
* DataSource dataSource =
* new ComboPooledDataSource("helloc3p0");
* 3. 从 DataSource 实例中获取数据库连接.
*/
@Test
public void testC3poWithConfigFile() throws Exception{
DataSource dataSource =
new ComboPooledDataSource("helloc3p0");
System.out.println(dataSource.getConnection());
ComboPooledDataSource comboPooledDataSource =
(ComboPooledDataSource) dataSource;
System.out.println(comboPooledDataSource.getMaxStatements());
}
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <named-config name="helloc3p0"> <!-- 指定连接数据源的基本属性 --> <property name="user">root</property> <property name="password">1230</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///atguigu</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>
原文:http://www.cnblogs.com/linyueshan/p/5608882.html