c3p0连接池的用法

1、导入c3p0jar包

c3p0-0.9.1.2.jar

2、xml配置文件

在src的目录下, 创建一个名称为 c3p0-config.xml的配置文件, 配置信息如下:

1)最基本配置*四个连接数据库必须的参数)

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
	<default-config>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://106.13.43.205:3306/test</property>
		<property name="user">root</property>
		<property name="password">aaaaa123</property>
	</default-config> 
</c3p0-config>

2)复杂的配置信息

<?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/test</property>  
        <property name="user">root</property>  
        <property name="password">123456</property>  
             <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default:3 -->  
        <property name="acquireIncrement">5</property>  
             <!--初始化的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3-->  
        <property name="initialPoolSize">10</property>  
             <!--连接池中保留的最小连接数-->  
        <property name="minPoolSize">5</property>  
             <!--连接池中保留的最大连接数。Default:15 -->  
        <property name="maxPoolSize">20</property>  
             <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->  
        <property name="acquireRetryAttempts">30</property>  
             <!--两次连接中间隔时间,单位毫秒。Default: 1000 -->  
        <property name="acquireRetryDelay">1000</property>  
             <!--连接关闭时默认将所有未提交的操作回滚。Default: false -->  
         <property name="autoCommitOnClose">false</property>  
    </default-config> 
</c3p0-config>

3、创建数据源DataSourceUtils(工具类)

package com.canghe.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DataSourceUtils {

	private static DataSource dataSource = new ComboPooledDataSource();

	private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

	// 直接可以获取一个连接池
	public static DataSource getDataSource() {
		return dataSource;
	}

	// 获取连接对象
	public static Connection getConnection() throws SQLException {

		Connection con = tl.get();
		if (con == null) {
			con = dataSource.getConnection();
			tl.set(con);
		}
		return con;
	}

	// 开启事务
	public static void startTransaction() throws SQLException {
		Connection con = getConnection();
		if (con != null) {
			con.setAutoCommit(false);
		}
	}

	// 事务回滚
	public static void rollback() throws SQLException {
		Connection con = getConnection();
		if (con != null) {
			con.rollback();
		}
	}

	// 提交并且 关闭资源及从ThreadLocall中释放
	public static void commitAndRelease() throws SQLException {
		Connection con = getConnection();
		if (con != null) {
			con.commit(); // 事务提交
			con.close();// 关闭资源
			tl.remove();// 从线程绑定中移除
		}
	}

	// 关闭资源方法
	public static void closeConnection() throws SQLException {
		Connection con = getConnection();
		if (con != null) {
			con.close();
		}
	}

	public static void closeStatement(Statement st) throws SQLException {
		if (st != null) {
			st.close();
		}
	}

	public static void closeResultSet(ResultSet rs) throws SQLException {
		if (rs != null) {
			rs.close();
		}
	}

}

4、使用DButils进行连接并对数据库进行操作

QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from product";
		List<Product> productList = null;
		try {
			productList = runner.query(sql, new BeanListHandler<Product>(Product.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}