JavaWeb6.1【数据库连接池、C3P0数据库连接池技术】
1 package com.haifei.datasource.c3p0; 2 3 import com.mchange.v2.c3p0.ComboPooledDataSource; 4 5 import javax.sql.DataSource; 6 import java.sql.Connection; 7 import java.sql.SQLException; 8 9 /* 10 数据库连接池 11 12 1. 概念:其实就是一个容器(集合),存放数据库连接的容器。 13 当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完之后,会将连接对象归还给容器。 14 15 2. 好处: 16 1. 节约资源 17 2. 用户访问高效 18 19 3. 实现: 20 1. 标准接口:DataSource javax.sql包下的 21 1. 方法: 22 * 获取连接:getConnection() 23 * 归还连接:Connection.close()。如果连接对象Connection是从连接池中获取的,那么调用Connection.close()方法,则不会再关闭连接了。而是归还连接 24 25 2. 一般我们不去实现它,有数据库厂商来实现 26 1. C3P0:数据库连接池技术 27 2. Druid:数据库连接池实现技术,由阿里巴巴提供的 28 29 4. C3P0:数据库连接池技术 30 1. 导入jar包 (两个) c3p0-0.9.5.2.jar 和 mchange-commons-java-0.2.12.jar , 31 * 不要忘记导入数据库驱动jar包mysql-connector-java-5.1.37-bin.jar 32 * 即一共所需3个jar包 33 2. 定义配置文件: 34 * 名称: c3p0.properties 或者 c3p0-config.xml 35 * 路径:直接将文件放在src目录下即可。 36 37 3. 创建核心对象 数据库连接池对象 ComboPooledDataSource 38 4. 获取连接: getConnection 39 */ 40 public class C3P0Demo1 { 41 public static void main(String[] args) throws SQLException { 42 //1 创建数据库连接池对象 43 DataSource ds = new ComboPooledDataSource(); 44 45 //2 获取连接对象 46 Connection conn = ds.getConnection(); 47 System.out.println(conn); //com.mchange.v2.c3p0.impl.NewProxyConnection@550ee7e5 [wrapping: com.mysql.jdbc.JDBC4Connection@5f9b2141] 48 49 50 } 51 }
1 package com.haifei.datasource.c3p0; 2 3 import com.mchange.v2.c3p0.ComboPooledDataSource; 4 5 import javax.sql.DataSource; 6 import java.sql.Connection; 7 import java.sql.SQLException; 8 9 public class C3P0Demo2 { 10 public static void main(String[] args) throws SQLException { 11 // testDefaultConfig(); 12 testNamedConfig(); 13 } 14 15 16 public static void testDefaultConfig() throws SQLException{ 17 //获取数据库连接池对象,使用默认配置<default-config> 18 DataSource ds = new ComboPooledDataSource(); 19 20 // for (int i=1; i<=10; i++){ 21 for (int i=1; i<=11; i++){ 22 Connection conn = ds.getConnection(); 23 System.out.println(i + ":" + conn); 24 25 /*if(i == 5){ 26 conn.close(); //#11号之前#5提前主动归还连接对象 27 }*/ 28 } 29 30 /* 31 1:com.mchange.v2.c3p0.impl.NewProxyConnection@3514a4c0 [wrapping: com.mysql.jdbc.JDBC4Connection@212b5695] 32 2:com.mchange.v2.c3p0.impl.NewProxyConnection@69997e9d [wrapping: com.mysql.jdbc.JDBC4Connection@793be5ca] 33 3:com.mchange.v2.c3p0.impl.NewProxyConnection@37654521 [wrapping: com.mysql.jdbc.JDBC4Connection@15aab8c6] 34 4:com.mchange.v2.c3p0.impl.NewProxyConnection@4de4b452 [wrapping: com.mysql.jdbc.JDBC4Connection@50b5ac82] 35 5:com.mchange.v2.c3p0.impl.NewProxyConnection@6babf3bf [wrapping: com.mysql.jdbc.JDBC4Connection@3059cbc] 36 6:com.mchange.v2.c3p0.impl.NewProxyConnection@24fcf36f [wrapping: com.mysql.jdbc.JDBC4Connection@10feca44] 37 7:com.mchange.v2.c3p0.impl.NewProxyConnection@ea6147e [wrapping: com.mysql.jdbc.JDBC4Connection@4d02f94e] 38 8:com.mchange.v2.c3p0.impl.NewProxyConnection@1e683a3e [wrapping: com.mysql.jdbc.JDBC4Connection@2053d869] 39 9:com.mchange.v2.c3p0.impl.NewProxyConnection@14555e0a [wrapping: com.mysql.jdbc.JDBC4Connection@4bb33f74] 40 10:com.mchange.v2.c3p0.impl.NewProxyConnection@3c73951 [wrapping: com.mysql.jdbc.JDBC4Connection@3d5c822d] 41 42 //Exception in thread "main" java.sql.SQLException: An attempt by a client to checkout a Connection has timed out. 43 */ 44 } 45 46 47 public static void testNamedConfig() throws SQLException{ 48 //获取数据库连接池对象,使用指定配置<named-config name="otherc3p0"> 49 DataSource ds = new ComboPooledDataSource("otherc3p0"); 50 51 for (int i=1; i<=10; i++){ 52 Connection conn = ds.getConnection(); 53 System.out.println(i + ":" + conn); 54 } 55 56 /* 57 1:com.mchange.v2.c3p0.impl.NewProxyConnection@6bedbc4d [wrapping: com.mysql.jdbc.JDBC4Connection@932bc4a] 58 2:com.mchange.v2.c3p0.impl.NewProxyConnection@2fd1433e [wrapping: com.mysql.jdbc.JDBC4Connection@29d89d5d] 59 3:com.mchange.v2.c3p0.impl.NewProxyConnection@212b5695 [wrapping: com.mysql.jdbc.JDBC4Connection@446293d] 60 4:com.mchange.v2.c3p0.impl.NewProxyConnection@793be5ca [wrapping: com.mysql.jdbc.JDBC4Connection@2df9b86] 61 5:com.mchange.v2.c3p0.impl.NewProxyConnection@15aab8c6 [wrapping: com.mysql.jdbc.JDBC4Connection@33990a0c] 62 6:com.mchange.v2.c3p0.impl.NewProxyConnection@50b5ac82 [wrapping: com.mysql.jdbc.JDBC4Connection@101952da] 63 7:com.mchange.v2.c3p0.impl.NewProxyConnection@3059cbc [wrapping: com.mysql.jdbc.JDBC4Connection@7ea9e1e2] 64 8:com.mchange.v2.c3p0.impl.NewProxyConnection@10feca44 [wrapping: com.mysql.jdbc.JDBC4Connection@3fb1549b] 65 66 Exception in thread "main" java.sql.SQLException: An attempt by a client to checkout a Connection has timed out. 67 */ 68 } 69 70 71 }
<c3p0-config> <!-- 使用默认的配置读取连接池对象 --> <default-config> <!-- 连接参数 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/hm_db2</property> <property name="user">root</property> <property name="password">root</property> <!-- 连接池参数 --> <property name="initialPoolSize">5</property><!-- 初始化申请的连接数量 --> <property name="maxPoolSize">10</property><!-- 最大的连接数量 --> <property name="checkoutTimeout">3000</property><!-- 超时时间 --> </default-config> <named-config name="otherc3p0"> <!-- 连接参数 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/hm_db2</property> <property name="user">root</property> <property name="password">root</property> <!-- 连接池参数 --> <property name="initialPoolSize">5</property> <property name="maxPoolSize">8</property> <property name="checkoutTimeout">1000</property> </named-config> </c3p0-config>