1.•当数据库访问结束后,程序还是像以前一样关闭数据库连接:conn.close();
但上面的代码并没有关闭数据库的物理连接,它仅仅把数据库连接释放,归还给了数据库连接池。
public class TestC3P0 {
/**
* 手动创建c3p0数据库连接池
* 参考:file:///E:/BaiduNetdiskDownload/c3p0-0.9.1.2/doc/index.html#quickstart文档
* @throws PropertyVetoException
* @throws SQLException
*/
@Test
public void test() throws PropertyVetoException, SQLException {
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "oracle.jdbc.driver.OracleDriver" );
cpds.setJdbcUrl( "jdbc:oracle:thin:@10.0.7.208:1521:ORCL" );
cpds.setUser("okaya10");
cpds.setPassword("okaya10");
System.out.println(cpds.getConnection());
}
/**
* 1.通过配置文件创建c3p0数据库连接池
* 参考:file:///E:/BaiduNetdiskDownload/c3p0-0.9.1.2/doc/index.html#configuration_files文档
* 2.创建ComboPooledDataSource实例:DataSource cpds = new ComboPooledDataSource("helloC3P0");
* 3.从DataSource获取连接池连接。
* @throws Exception
*/
@Test
public void testC3P0() throws Exception {
DataSource cpds = new ComboPooledDataSource("helloC3P0");
System.out.println(cpds.getConnection());
ComboPooledDataSource comboPooledDataSource = (ComboPooledDataSource) cpds;
System.out.println(comboPooledDataSource.getMaxPoolSize());
}
}
2. c3p0配置文件。
<c3p0-config>
<!-- This app is massive! -->
<named-config name="helloC3P0">
<!-- 指定连接数据源的基本属性 -->
<property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
<property name="jdbcUrl">jdbc:oracle:thin:@10.0.7.208:1521:ORCL</property>
<property name="user">okaya10</property>
<property name="password">okaya10</property>
<!-- 若连接池中得连接不足时,一次向数据库服务器申请多少个连接 -->
<property name="acquireIncrement">50</property>
<!-- 初始化数据库连接池时连接的数量 -->
<property name="initialPoolSize">5</property>
<!-- 数据库连接池中最小的连接数-->
<property name="minPoolSize">50</property>
<!-- 数据库连接池中最大的连接数量 -->
<property name="maxPoolSize">1000</property>
<!-- c3p0数据库连接池可以维护的statements的个数 -->
<property name="maxStatements">20</property>
<!-- 每个连接同时可以使用的statement的个数。 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>