一、C3P0
c3p0是一个开源的JDBC连接池,它相对于DBCP和Druid来说有一个更方便的配置连接方式,就是自动读取配置文件。
二、C3P0的连接方式一
jar包下载地址:https://mvnrepository.com/artifact/c3p0/c3p0/0.9.1.2
package com.jun.jdbc.datasource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;
public class C3P001 {
public static void main(String[] args) throws Exception {
C3P001 c3P001 = new C3P001();
c3P001.testC3P0_01();
}
//方式1:相关参数,在程序中指定user,url,password等
public void testC3P0_01() throws Exception {
//创建一个数据源对象
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
//通过配置文件mysql.properties获取相关连接的信息
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
//读取相关属性
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driver = properties.getProperty("driver");
//给数据源comboPooledDataSource设置相关参数
//连接管理是由comboPooledDataSource来管理
comboPooledDataSource.setDriverClass(driver);
comboPooledDataSource.setJdbcUrl(url);
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(password);
//设置初始化连接数
comboPooledDataSource.setInitialPoolSize(5);
//最大连接数
comboPooledDataSource.setMaxPoolSize(50);
long start = System.currentTimeMillis();
for (int i = 0; i <5000 ; i++) {
Connection connection = comboPooledDataSource.getConnection();//由DataSuore接口实现
connection.close();
}
long end = System.currentTimeMillis();
System.out.println("C3P0 5000次连接耗时="+(end-start));
}
}
三、C3P0的连接方式二
c3p0-config.xml配置文件
<c3p0-config>
<!--数据库名称代表连接池-->
<named-config name="jun">
<!--驱动类-->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<!--url -->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jun_db02</property>
<!--用户名-->
<property name="user">root</property>
<!--密码-->
<property name="password">654321</property>
<!--每次增长连接数-->
<property name="acquireIncrement">5</property>
<!--初始连接数-->
<property name="initialPoolSize">10</property>
<!--最小连接数-->
<property name="minPoolSize">5</property>
<!--最大连接数-->
<property name="maxPoolSize">50</property>
<!--可连接的最多的命令对象数-->
<property name="maxStatements">5</property>
<!--每个连接对象可连接的最多的命令对象数-->
<property name="maxStatementsPerConnection">2</property>
</named-config>
</c3p0-config>
package com.jun.jdbc.datasource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
public class C3P002 {
//方式2:使用配置文件模板来完成
//将c3p0提供的c3p0.config.xml考到src目录下
//这个文件指定了连接数据库和连接池的相关参数
@Test
public void testC3P0_02() throws SQLException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("jun");
long start = System.currentTimeMillis();
System.out.println("计时开始...");
for (int i = 0; i <5000 ; i++) {
Connection connection = comboPooledDataSource.getConnection();
// System.out.println("连接成功");
connection.close();
}
long end = System.currentTimeMillis();
System.out.println("c3p0连接方式二耗时="+(end-start));//c3p0连接方式二耗时=481
}
}