一、导入依赖
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
二、参数配置
涉及到地址jdbcUrl、数据库驱动jdbcDriver、用户名userName、用户密码password、初始创建连接数initPoolSize、最大连接数maxPoolSize、最小连接数minPoolSize、最大空闲时间maxIdleTime的配置。然后创建ComboPooledDataSource实例作为连接池的数据源对象,并为这个实例设置参数。
三、获取连接
ComboPooledDataSource实例调用getConnect方法。
关于连接池中连接的分配:
当接收到数据库连接请求时,先看连接池中是否有空闲连接,如果有,就将连接分配来使用;如果没有,先看当前所开的连接数是否达到连接池的最大连接数量,如果没达到就重新创建默认因子个连接(acquireIncrement默认值为3)给请求的客户,如果达到,请求就按最大等待时间进行等待,等待前面的连接使用完后归还到连接池中,再使用。这种方式可以保证连接的合理利用,避免不必要的开销。
四、使用mysql进行操作
和jdbc连接数据库一样,对数据库进行select、insert、delete等基本操作。
五、关闭连接
调用close方法,此处并未真正杀死连接,而是将连接归还到连接池中,以达到重复利用的目的。
六、关闭连接池
连接池内部有会检测所有连接空闲时间的定时程序,如果空闲时间大于连接池的最大空闲时间,就会将这个连接杀死,同时还要保证连接池中连接的数量不能小于最小连接数量。
七、代码示例
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class PoolTest {
private String jdbcUrl;
private String jdbcDriver;
private String userName;
private String password;
private String initPoolSize;
private String maxPoolSize;
private String minPoolSize;
private String maxIdleTime;
private Properties pro;
private ComboPooledDataSource dataSource;
public PoolTest(){
pro=new Properties();
//创建实例作为c3p0连接池的数据源对象
dataSource=new ComboPooledDataSource();
try {//参数配置
pro.load(new FileInputStream("D:\\db.properties"));
jdbcDriver = pro.getProperty("jdbcDriver");
jdbcUrl = pro.getProperty("jdbcUrl");
userName = pro.getProperty("userName");
password = pro.getProperty("password");
initPoolSize=pro.getProperty("initialPoolSize");
maxPoolSize=pro.getProperty("maxIdleTime");
minPoolSize=pro.getProperty("minPoolSize");
maxIdleTime=pro.getProperty("maxPoolSize");
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setDriverClass(jdbcDriver);
dataSource.setUser(userName);
dataSource.setPassword(password);
dataSource.setMaxIdleTime(Integer.parseInt(maxIdleTime));
dataSource.setInitialPoolSize(Integer.parseInt(initPoolSize));
dataSource.setMaxPoolSize(Integer.parseInt(maxPoolSize));
dataSource.setMinPoolSize(Integer.parseInt(minPoolSize));
} catch (IOException e) {
e.printStackTrace();
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
public Connection getConnection(){
Connection con=null;
try {
con= dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
//模拟用户使用mysql进行查询操作
public static void main(String[] args) {
PoolTest p=new PoolTest();
Connection connection=p.getConnection();
String sql="select * from table1 where id>=?";
PreparedStatement pre=null;
try {
pre=connection.prepareStatement(sql);
pre.setInt(1,3);//查看id大于等于3的
ResultSet resultSet = pre.executeQuery();//获取结果集
//遍历结果集查看获取数据
//此处next方法类似迭代器的next和hasNext的结合实现
while(resultSet.next()) {
System.out.println("id:" + resultSet.getString(1) +
" age:" + resultSet.getString(2)); }
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
pre.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}