C3P0开源免费的连接池!目前使用它的开源项目有:Spring、Hibernate等。使用第三方工具需要导入jar包,C3P0使用时还需要添加配置文件从c3p0-config.xml
1、导入jar包
2、配置文件
1)、配置文件名称:c3p0-config.xml(固定)
2)、配置文件位置:src(类路径)
3)、配置文件内容:命名配置:
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/stud</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config>
<named-config name="yuye">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/stud</property>
<property name="user">root</property>
<property name="password">root</property>
</named-config>
</c3p0-config>
3、常见配置项:
分类 | 属性 | 描述 |
必须项 | user | 用户名 |
| password | 密码 |
| driverClass | 驱动 Mysql:com.mysql.jdbc.Driver |
| jdbcUrl | 路径 Mysql路径:jdbc:mysql://localhost:3306/数据库 |
基本配置 | acquireIncrement | 连接池无空闲连接可用时,一次性创建的连接数 默认值:3 |
| initialPoolSize | 连接池初始化时创建的连接数 默认值:3 |
| maxPoolSize | 连接池中拥有的最大连接数 默认值:15 |
| minPoolSize | 连接池保存的最小连接数 |
| maxIdleTime | 连接的最大空闲时间。如果超过这个时间,某个数据库连接还没有使用,则会断开掉这个连接,如果为0,则永远不会断开。 默认值:0 |
管理连接池的大小和连接的生存时间(扩展) | maxConnectionAge | 配置连接的生存时间,超过这个时间的连接将由连接池自动断开丢弃掉。当然正在使用的连接不会马上断开,而是等待close再断开。配置为0的时候则不会对连接的生存时间进行限制。 默认值:0 |
| maxIdleTimeExcessConnections | 这个配置主要是为了减轻连接池的负载,配置不为0,则会将连接池中的连接数量保持到minPoolSize,为0则不处理。 |
配置PreparedStatement缓存(扩展) | maxStatements | 连接池为了数据源缓存的PreparedStatement总数。由于PreparedStatement术语单个Connection,所以这个数量应该根据应用中的平均连接数乘以每个连接的平均PreparedStatement来计算。为0的时候不缓存,同时maxStatementsPerConnection的配置无效 |
| maxStatementPerConnection | 连接池为数据源单个Connection缓存的PreparedStatement数,这个配置比maxStatements更有意义,因为它的缓存的服务对象是单个数据连接,如果设置的好,肯定是可以提高性能的。为0的时候不缓存。 |
4、工具类
C3P0提供核心工具类:ComboPooledDataSource,如果要使用连接池,必须创建该类的实例对象。
1、new ComboPooledDataSource(“名称”);使用配置文件“命名配置”
<named-config name=”yuye”>
2、new ComboPooledDataSource();使用配置文件“默认配置”
<default-config>
例:
public class TestC3P0 {
@Test
public void Test() {
ComboPooledDataSource datasource = new ComboPooledDataSource("yuye");
PreparedStatement pstmt = null;
ResultSet res = null;
Connection conn = null;
try {
conn = datasource.getConnection();
pstmt = conn.prepareStatement("select * from student");
res = pstmt.executeQuery();
while(res.next()) {
System.out.println(res.getString(1)+"\t"+res.getString(2)+"\t"+res.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCutil.close(conn, pstmt, res);
}
}
}
5、C3P0DataSourceUtil
package cn.yuchao.utils;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DataSourceUtil {
private static DataSource dataSource = new ComboPooledDataSource();
//线程局部变量
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
//直接得到连接
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
//得到数据库连接池
public static DataSource getDataSource() {
return dataSource;
}
//获得线程变量中的connection
public static Connection getCurrentConnetcion() throws SQLException {
Connection conn = tl.get();
//第一次获取为空,创建
if(conn == null) {
conn = getConnection();
tl.set(conn);
}
return conn;
}
//开启事务
public static void startTransaction() throws SQLException {
Connection conn = getCurrentConnetcion();
if(conn != null)
conn.setAutoCommit(false);
}
//回滚事务
public static void rollback() throws SQLException {
Connection conn = getCurrentConnetcion();
if(conn != null)
conn.rollback();
}
//提交事务、关闭资源、移出线程局部变量中的connection
public static void commit() throws SQLException {
Connection conn = getCurrentConnetcion();
if(conn!=null) {
conn.commit();
tl.remove();
conn.close();
}
}
}