什么是C3P0连接池:
开源的JDBC连接池,C3P0连接池是在程序操作数据库之前预先根据配置文件创建一定数量的连接,当线程需要时直接取走,缩短了创建连接的时间,当使用完毕后,释放连接后放回连接池,以此类推,如果连接池中的连接使用完后,程序会根据配置文件配置的数据再次创建一批,使用完后放回连接池,并不是真正的关闭连接。
C3P0和DHCP对比:
DHCP没有自动回收空闲连接的功能
C3P0有自动回收空闲连接功能
C3P0连接池的重要性:
在Java开发中连接数据库使用到的场景很多,一般我们在项目中每次操作数据库都要建立一次连接,由于JDBC没有保持连接的能力,超时后将会释放连接,每次建立连接大约需要140毫秒左右(取决于计算机性能),这样一来就会发回大量的时间,降低工作效率。如果使用C3P0连接池,直接取走连接大大缩短创建连接的时间。
实例
导入JAR包,有本例子使用的是maven工程,所以只需要导入相应的定位
<!-- mysql驱动-->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.17</version>
</dependency>
<!-- junit测试-->
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>RELEASE</version><scope>compile</scope>
</dependency>
<!-- c3p0-->
<dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version>
</dependency>
<!-- log4j日志-->
<dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version>
</dependency>
连接类:(不使用配置文件)
package UtillTools;import com.mchange.v2.c3p0.ComboPooledDataSource;import org.junit.Test;import java.beans.PropertyVetoException;import java.sql.Connection;import java.sql.SQLException;public class C3P0Data {/*** 在类里面加载各个参数,不适用配置文件* @throws PropertyVetoException* @throws SQLException*/@Testpublic void getSimpleConnect() throws PropertyVetoException, SQLException {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/tb_school_system");dataSource.setUser("root");dataSource.setPassword("123456");Connection conn=dataSource.getConnection();System.out.println(conn);conn.close();}}
连接成功如图:

连接类:(实用配置文件)
配置文件c3p0-config.xml必须使用此文件名,否则无法加载
注意:网上有很多说配置文件放到src下面就行,但是作者在使用IDEA(maven工程)测试时并不不行,最后放到resources下才能成功读取。
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config><!--默认配置--><default-config><property name="driverClass">com.mysql.cj.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/tb_school_system</property><property name="user">root</property><property name="password">ch92725931125</property><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">100</property><property name="minPoolSize">10</property><property name="maxStatements">200</property><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">100</property><property name="minPoolSize">10</property><property name="maxStatements">200</property></default-config><!--配置连接池mysql--><named-config name="mysql"><property name="driverClass">com.mysql.cj.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/tb_school_system</property><property name="user">root</property><property name="password">ch92725931125</property><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">100</property><property name="minPoolSize">10</property><property name="maxStatements">200</property></named-config><!--配置连接池2-->
<!-- <named-config name="oracle-config">-->
<!-- <property name="driverClass">com.mysql.jdbc.Driver</property>-->
<!-- <property name="jdbcUrl">jdbc:oracle:thin:@地址:端口:ORCL</property>-->
<!-- <property name="user">root</property>-->
<!-- <property name="password">123456</property>-->
<!-- <property name="initialPoolSize">10</property>-->
<!-- <property name="maxIdleTime">30</property>-->
<!-- <property name="maxPoolSize">100</property>-->
<!-- <property name="minPoolSize">10</property>-->
<!-- <property name="maxStatements">200</property>-->
<!-- </named-config>--></c3p0-config>
C3P0连接类
package UtillTools;import com.mchange.v2.c3p0.ComboPooledDataSource;import org.junit.Test;import java.beans.PropertyVetoException;import java.sql.Connection;import java.sql.SQLException;import java.util.logging.Logger;public class C3P0Config {/*** * @throws PropertyVetoException* @throws SQLException*/static Logger logger=Logger.getLogger(C3P0Config.class.getName());
/*加载配置文件中named-config name的值(通过此值来区分配置那一套数据库连接),如果为空则使用xml文件中的default-config
*/static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql");@Testpublic static Connection getMysqlConfigConnect() {try {return dataSource.getConnection();} catch (SQLException e) {e.printStackTrace();}return null;}//释放资源
public static void getCloseConnect(ResultSet res, PreparedStatement pres,Connection conn) {if(res !=null){try {res.close();} catch (SQLException e) {e.printStackTrace();}}if (pres != null){try {pres.close();} catch (SQLException e) {e.printStackTrace();}}if(conn != null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}
}public static void main(String[] args) {C3P0Config.getMysqlConfigConnect();}}
测试结果:

如有不足请指正,更多资料请关注微信公众号

请大家多多支持!















