之前已经讲过dbcp可以用于数据库连接池进行管理。另一种技术c3p0也可以用于数据库连接池管理,其中Spring等框架都是基于c3p0技术进行数据库连接池管理的。

使用之前需要引入 c3p0-0.9.5.2.jar 和 mchange-commons-java-0.2.11.jar 包,主要的类是ComboPooledDataSource,也有两种方式进行设置。一种是代码中进行设置,一种是在配置文件中设置。主要区别就是这种方式只有一个主要类ComboPooledDataSource。

他们之间可能方法名有所不同,但是功能是一样的。

1)在代码中进行配置,代码如下

ComboPooledDataSource dataSource = new ComboPooledDataSource();

        try {
dataSource.setJdbcUrl("jdbc:mysql:///mydb?useSSL=true");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setUser("root");
dataSource.setPassword("");
dataSource.setMaxIdleTime();
dataSource.setMaxPoolSize();
dataSource.setInitialPoolSize();
Connection conn = dataSource.getConnection(); String sql = "select * from user where id=?";
QueryRunner qr = new QueryRunner();
User user = qr.query(conn, sql, new BeanHandler<User>(User.class), ); System.out.println(user); dataSource.close(); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

2) 在配置文件中进行设置

注意:配置文件命名是 c3p0-config.xml。系统默认回去 CLASSPATH、WEB-INF/classes文件夹中搜索配置文件,所以把他放在 src 目录下即可。

ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql_config");   //使用指定配置名的配置
try {
Connection conn = dataSource.getConnection();
String sql = "select * from user where id=?";
QueryRunner qr = new QueryRunner();
User user = qr.query(conn, sql, new BeanHandler<User>(User.class), ); System.out.println(user); dataSource.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

配置文件中可以进行设置的参数如下:

acquireIncrement
acquireRetryAttempts
acquireRetryDelay
autoCommitOnClose
automaticTestTable
breakAfterAcquireFailure
checkoutTimeout
connectionCustomizerClassName
connectionTesterClassName
contextClassLoaderSource
dataSourceName
debugUnreturnedConnectionStackTraces
driverClass
extensions
factoryClassLocation
forceIgnoreUnresolvedTransactions
forceSynchronousCheckins
forceUseNamedDriverClass
idleConnectionTestPeriod
initialPoolSize
jdbcUrl
maxAdministrativeTaskTime
maxConnectionAge
maxIdleTime
maxIdleTimeExcessConnections
maxPoolSize
maxStatements
maxStatementsPerConnection
minPoolSize
numHelperThreads
overrideDefaultUser
overrideDefaultPassword
password
preferredTestQuery
privilegeSpawnedThreads
propertyCycle
statementCacheNumDeferredCloseThreads
testConnectionOnCheckin
testConnectionOnCheckout
unreturnedConnectionTimeout
user

配置文件样例如下

<c3p0-config>
<default-config>
<property name="checkoutTimeout"></property>
<property name="idleConnectionTestPeriod"></property>
<property name="initialPoolSize"></property>
<property name="maxIdleTime"></property>
<property name="maxPoolSize"></property>
<property name="minPoolSize"></property> <user-overrides user="test-user">
<property name="maxPoolSize"></property>
<property name="minPoolSize"></property>
<property name="maxStatements"></property>
</user-overrides> </default-config>
<named-config name="mysql_config">
<property name="initialPoolSize"></property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb?useSSL=true</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="maxPoolSize"></property>
<property name="initialPoolSize"></property>
<property name="maxIdleTime"></property>
<property name="password"></property>
<property name="user">root</property> </named-config> </c3p0-config>
05-11 22:52