C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展,设计非常简单易用
添加maven依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5</version> </dependency>
编写C3P0工具类
public class C3P0Utils { private static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql"); public static DataSource getDataSource() { return dataSource; } public static Connection getConnection() { try { return dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException(e); } } //释放连接回连接池 public static void close(Connection conn, PreparedStatement pst, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pst != null) { try { pst.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
classpath下面配置文件
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://192.168.47.151:3306/web</property> <property name="user">root</property> <property name="password">root</property> <property name="initialPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> <named-config name="mysql"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://192.168.47.151:3306/web</property> <property name="user">root</property> <property name="password">root</property> </named-config> </c3p0-config>
测试代码
@Test public void test1(){ Connection conn = null; PreparedStatement pstmt = null; // 1.创建自定义连接池对象 DataSource dataSource = new DataSourcePool(); try { // 2.从池子中获取连接 conn = C3P0Utils.getConnection(); String sql = "insert into USER values(?,?)"; //3.必须在自定义的connection类中重写prepareStatement(sql)方法 pstmt = conn.prepareStatement(sql); pstmt.setString(1, "李四"); pstmt.setString(2, "1234"); int rows = pstmt.executeUpdate(); System.out.println("rows:"+rows); } catch (Exception e) { throw new RuntimeException(e); } finally { C3P0Utils.close(conn,pstmt,null); } }
C3P0的其他细化配置
<!--acquireIncrement:链接用完了自动增量3个。 --> <property name="acquireIncrement">3</property> <!--acquireRetryAttempts:链接失败后重新试30次。--> <property name="acquireRetryAttempts">30</property> <!--acquireRetryDelay;两次连接中间隔1000毫秒。 --> <property name="acquireRetryDelay">1000</property> <!--autoCommitOnClose:连接关闭时默认将所有未提交的操作回滚。 --> <property name="autoCommitOnClose">false</property> <!--automaticTestTable:c3p0测试表,没什么用。--> <property name="automaticTestTable">Test</property> <!--breakAfterAcquireFailure:出错时不把正在提交的数据抛弃。--> <property name="breakAfterAcquireFailure">false</property> <!--checkoutTimeout:100毫秒后如果sql数据没有执行完将会报错,如果设置成0,那么将会无限的等待。 --> <property name="checkoutTimeout">100</property> <!--connectionTesterClassName:通过实现ConnectionTester或QueryConnectionTester的类来测试连接。类名需制定全路径。Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester--> <property name="connectionTesterClassName"></property> <!--factoryClassLocation:指定c3p0 libraries的路径,如果(通常都是这样)在本地即可获得那么无需设置,默认null即可。--> <property name="factoryClassLocation">null</property> <!--forceIgnoreUnresolvedTransactions:作者强烈建议不使用的一个属性。--> <property name="forceIgnoreUnresolvedTransactions">false</property> <!--idleConnectionTestPeriod:每60秒检查所有连接池中的空闲连接。--> <property name="idleConnectionTestPeriod">60</property> <!--initialPoolSize:初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。 --> <property name="initialPoolSize">3</property> <!--maxIdleTime:最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。--> <property name="maxIdleTime">60</property> <!--maxPoolSize:连接池中保留的最大连接数。 --> <property name="maxPoolSize">15</property> <!--maxStatements:最大链接数。--> <property name="maxStatements">100</property> <!--maxStatementsPerConnection:定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --> <property name="maxStatementsPerConnection"></property> <!--numHelperThreads:异步操作,提升性能通过多线程实现多个操作同时被执行。Default: 3--> <property name="numHelperThreads">3</property> <!--overrideDefaultUser:当用户调用getConnection()时使root用户成为去获取连接的用户。主要用于连接池连接非c3p0的数据源时。Default: null--> <property name="overrideDefaultUser">root</property> <!--overrideDefaultPassword:与overrideDefaultUser参数对应使用的一个参数。Default: null--> <property name="overrideDefaultPassword">password</property> <!--password:密码。Default: null--> <property name="password"></property> <!--preferredTestQuery:定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意: 测试的表必须在初始数据源的时候就存在。Default: null--> <property name="preferredTestQuery">select id from test where id=1</property> <!--propertyCycle:用户修改系统配置参数执行前最多等待300秒。Default: 300 --> <property name="propertyCycle">300</property> <!--testConnectionOnCheckout:因性能消耗大请只在需要的时候使用它。Default: false --> <property name="testConnectionOnCheckout">false</property> <!--testConnectionOnCheckin:如果设为true那么在取得连接的同时将校验连接的有效性。Default: false --> <property name="testConnectionOnCheckin">true</property> <!--user:用户名。Default: null--> <property name="user">root</property> <!--usesTraditionalReflectiveProxies:动态反射代理。Default: false--> <property name="usesTraditionalReflectiveProxies">false</property>