C3P0数据库连接池的java实现
1、配置准备
- 导入jar包
- c3p0-0.9.2-pre1.jar
- mchange-commons-0.2.jar
- 数据库驱动包,如:mysql-connector-java-5.1.28-bin.jar
- 配置文件名称必须叫c3p0-config.xml
- 配置文件位置必须在src下
2、配置文件
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- 这是默认配置信息 --> <default-config> <!-- 连接四大参数配置 --> <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb3</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">123</property> <!-- 池参数配置 --> <!-- 连接池初始化时建立的连接数 默认值是3 --> <property name="initialPoolSize">3</property> <!-- 连接的最大空闲时间 单位秒 默认是0-代表永远不会断开连接 超过设定时间的空闲连接将会断开 --> <property name="maxIdleTime">30</property> <!-- 连接池中拥有的最大连接数 默认值为15个 --> <property name="maxPoolSize">20</property> <!-- 连接池中保持的最小连接数 默认值为3个--> <property name="minPoolSize">3</property> <!-- 将连接池的连接数保持在minpoolsize 必须小于maxIdleTime设置 默认值为0代表不处理 单位秒 --> <property name="maxIdleTimeExcessConnections">15</property> </default-config> <!-- 专门为oracle提供的配置信息 --> <named-config name="oracle-config"> <property name="jdbcUrl">jdbc:oracle:oci8:@dataname</property> <property name="driverClass">oracle.jdbc.driver.OracleDriver</property> <property name="user">root</property> <property name="password">123</property> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </named-config> </c3p0-config>
3、实现案例
import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.SQLException; import org.junit.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; /** * c3p0*/ public class Demo1 { /** * 代码配置,该配置不需要配置文件 * @throws PropertyVetoException * @throws SQLException */ @Test public void fun1() throws PropertyVetoException, SQLException { // 创建连接池对象 ComboPooledDataSource dataSource = new ComboPooledDataSource(); // 对池进行四大参数的配置 dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb"); dataSource.setUser("root"); dataSource.setPassword("123"); // 池配置 dataSource.setAcquireIncrement(5); dataSource.setInitialPoolSize(20); dataSource.setMinPoolSize(2); dataSource.setMaxPoolSize(50); Connection con = dataSource.getConnection(); System.out.println(con); con.close(); //该close()方法不是关闭连接,而是将连接归还给连接池。 } /** * 配置文件的默认配置,若代码中同时存在配置,代码中的配置参数优先 * @throws SQLException */ @Test public void fun2() throws SQLException{ /** * 在创建连接池对象时,这个对象就会自动加载配置文件!不用我们来指定 */ ComboPooledDataSource dataSource = new ComboPooledDataSource(); Connection con = dataSource.getConnection(); System.out.println(con); con.close(); } /** * 使用命名配置信息 * @throws SQLException */ @Test public void fun3() throws SQLException{ /** * 构造器的参数指定命名配置元素的名称! * <named-config name="oracle-config"> */ ComboPooledDataSource dataSource = new ComboPooledDataSource("oracle-config"); Connection con = dataSource.getConnection(); System.out.println(con); con.close(); } }
作者:何海洋
本博客内容主要以学习、研究和分享为主,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。