先看官方对C3P0的介绍:C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展,目前在Hibernate和Spring中,均有使用。
工欲善其事必先利其器,先下相关的jar包吧,CSDN上一搜一大把,我的资源中也有。
C3P0就是一个数据库连接池,我们先看一个巨简单的使用C3P0的例子吧,访问本机Oracle的一张表:
package cn.test; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3P0Test { public static void main(String[] args) { try { // 创建一个数据源 ComboPooledDataSource cpds = new ComboPooledDataSource(); // 设置数据库的相关信息 cpds.setDriverClass("oracle.jdbc.driver.OracleDriver"); cpds.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:ORCL"); cpds.setUser("testdb"); cpds.setPassword("testdb"); cpds.setMaxStatements(180); cpds.setMaxPoolSize(100); Connection con = null; Statement stmt = null; ResultSet rs = null; con = cpds.getConnection(); stmt = con.createStatement(); rs = stmt.executeQuery("select id, name ,age, job, address from test_user "); while (rs.next()) { System.out.println("ID : " + rs.getString("id") + " , NAME : " + rs.getString("name") +" , AGE : " + rs.getInt("age") + " , JOB : " + rs.getString("job") + " , ADDRESS : " + rs.getString("address")); } } catch (Exception e) { e.printStackTrace(); } } }
最后的输出结果为:
2012-8-16 15:44:09 com.mchange.v2.log.MLog <clinit> 信息: MLog clients using java 1.4+ standard logging. 2012-8-16 15:44:10 com.mchange.v2.c3p0.C3P0Registry banner 信息: Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10] 2012-8-16 15:44:11 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager 信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 2rxpor8pcvddxxgwjcsb|e4f972, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> oracle.jdbc.driver.OracleDriver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 2rxpor8pcvddxxgwjcsb|e4f972, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:oracle:thin:@localhost:1521:ORCL, lastAcquisitionFailureDefaultUser -> null, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 100, maxStatements -> 180, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ] ID : 00000000000000000001 , NAME : tom , AGE : 40 , JOB : manager , ADDRESS : ny ID : 00000000000000000002 , NAME : jimmy , AGE : 35 , JOB : ceo , ADDRESS : huston ID : 00000000000000000003 , NAME : tim , AGE : 33 , JOB : cfo , ADDRESS : canada
输出前部分是C3P0自己往控制台输出的一些信息。最后3行是,该表在数据库中的3条记录。
在实际应用中,C3P0可以结合应用服务器提供的JNDI服务为业务应用提供数据库连接池对象,业务中可以通过这个对象获取数据库连接。如Tomcat本身内置了一个数据库连接池,我们可以通过C3P0进行替换!