package com.gxa.bj.util;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0connectionHelper {
private static DataSource ds;
//在线程中创建Connection对象的副本
private static ThreadLocal<Connection> t = new ThreadLocal<Connection>();
static{
ds = new ComboPooledDataSource();//会自动从src目录下载c3p1配置
}
/**
* 获取连接对象
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException{
Connection con = t.get();//得到一个线程变量的副本
if(con == null){
con = ds.getConnection();
t.set(con);//创建一个线程变量的副本
}
return con;
}
public static Connection getCloseConnection() throws SQLException{
Connection con = t.get();
if(con!=null){
con = ds.getConnection();
t.set(con);
}
return con;
}
}