记录c3p0数据库连接池的基本使用以及配置
首先导入c3p0的jar包
创建数据源对象
private static DataSource dataSource = null;
static{
dataSource = new ComboPooledDataSource("mvcApp");
}
使用数据源获取connection
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
public static void releaseConnection(Connection connection){
try{
if(connection != null){
connection.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
配置文件
配置文件需创建在src的目录下这样能方便调用
<c3p0-config>
<!--创建DataSource时传入的参数就是这个name-config 的name属性-->
<name-config name="mvcApp">
<!--链接数据库的基本配置-->
<property name="user">root</property>
<property name="password">252048917</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/ljs</property>
<!-- 连接不够则向数据库一次请求多少个连接-->
<property name="acquireIncrement">5</property>
<!--初始化的连接个数-->
<property name="initialPoolSize">10</property>
<!-- 连接到数据库但是超过这个时间没被使用则会断开连接 连接的最大空闲时间 如果为0则不会断开连接-->
<property name="maxIdleTime">30</property>
<!-- 最小最大连接数-->
<property name="maxPoolSize">50</property>
<property name="minPoolSize">10</property>
</name-config>
</c3p0-config>