匿名用户
1级
2012-08-10 回答
public class DBUtil {
static{
try {
Properties props = new Properties();
props.load(DBUtil.class.getClassLoader().getResourceAsStream("dbcp.properties"));
dataSource = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
e.printStackTrace();
}
}
private static DataSource dataSource;
private static ThreadLocal conLocal = new ThreadLocal();
public static Connection openConnection()throws SQLException{
Connection con = conLocal.get();
if(con == null){
con = dataSource.getConnection();
conLocal.set(con);
}
return con;
}
public static void closeConnection()throws SQLException{
Connection con = conLocal.get();
conLocal.set(null);
if(con != null && !con.isClosed()){
con.close();
}
}
}
------dbcp.properties---------------------------------------------------------------
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@10.87.66.144:1521:orcl
username=scott
password=111111
initialSize=3
maxActive=15
maxIdle=2
minIdle=1
maxWait=30000