Jar包:
下载地址:http://sourceforge.net/projects/c3p0/files/
或在Hibernate发布包中,最新为c3p0-0.9.2.jar
示例:
package c3p0; import java.sql.Connection; import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3p0ConnPool { private static ComboPooledDataSource cpDs; static { cpDs = new ComboPooledDataSource(); try { cpDs.setDriverClass("com.microsoft.jdbc.sqlserver.SQLServerDriver"); cpDs.setJdbcUrl("jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=sample"); cpDs.setUser("sa"); cpDs.setPassword("123"); cpDs.setMinPoolSize(10); cpDs.setMaxPoolSize(30); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection() { Connection conn = null; try{ conn = cpDs.getConnection(); }catch(Exception e){ } return conn; } public static void main(String[] args) { Connection conn = null; try { conn = C3p0ConnPool.getConnection(); // ... // use the connection // ... } catch (Exception e) { } finally { if (conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
附:[转载]c3p0连接池配置
最近试用了c3p0数据库连接池,配置方式使用的是propery文件,将c3p0.properties放在classpath目录下,如果为WEB应用,放在WEB-INF\classes下
# # This file is detritus from various testing attempts # the values below may change, and often do not represent # reasonable values for the parameters. # c3p0.jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:chenchi c3p0.driverClass=oracle.jdbc.driver.OracleDriver c3p0.user=hbota2 c3p0.password=ty1234 c3p0.minPoolSize=3 c3p0.maxPoolSize=20 #c3p0.testConnectionOnCheckout=true #c3p0.testConnectionOnCheckin=true #c3p0.checkoutTimeout=2000 #c3p0.idleConnectionTestPeriod=5 #c3p0.maxConnectionAge=10 #c3p0.maxIdleTime=2 #c3p0.maxIdleTimeExcessConnections=1 #c3p0.propertyCycle=1 #c3p0.numHelperThreads=10 #c3p0.unreturnedConnectionTimeout=15 #c3p0.debugUnreturnedConnectionStackTraces=true #c3p0.maxStatements=30 #c3p0.maxStatementsPerConnection=5 #c3p0.maxAdministrativeTaskTime=3 #c3p0.preferredTestQuery=SELECT 1 #c3p0.preferredTestQuery=SELECT a FROM emptyyukyuk WHERE a = 5 #c3p0.preferredTestQuery=SELECT a FROM testpbds WHERE a = 5 #c3p0.usesTraditionalReflectiveProxies=true #c3p0.automaticTestTable=PoopyTestTable #c3p0.acquireIncrement=4 #c3p0.acquireRetryDelay=1000 #c3p0.acquireRetryAttempts=60 #c3p0.connectionTesterClassName=com.mchange.v2.c3p0.test.AlwaysFailConnectionTester #c3p0.initialPoolSize=10 com.mchange.v2.log.MLog=com.mchange.v2.log.log4j.Log4jMLog #com.mchange.v2.log.MLog=com.mchange.v2.log.jdk14logging.Jdk14MLog #com.mchange.v2.log.MLog=com.mchange.v2.log.FallbackMLog com.mchange.v2.log.NameTransformer=com.mchange.v2.log.PackageNames com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL=ALL #com.mchange.v2.c3p0.VMID=poop
在java代码中直接使用即可
ComboPooledDataSource cpds = new ComboPooledDataSource(); Connection conn = cpds.getConnection(); //业务代码 conn.close();
如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。

