请高手指导我写的关于DBCP和ThreadLocal共同管理Connection放入代码
ThreadLocal 和 DBCP连接池 --->共同管理Connection的 工具类代码:
1. 从连接池中拿到Connection 并放入ThreadLocal中 管理:
package com.pccw.test.util;
import java.sql.*;
public class ConnectionUtils {
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
public static Connection getConnection(){
try{
Connection connection=threadLocal.get();
if(connection==null || connection.isClosed()){
connection=DBCPConnectionManager.getInstance().getConnection();
threadLocal.set(connection);
return connection;
}
}catch(SQLException e){
e.printStackTrace();
}
return null;
}
public static void closeConnection() {
Connection connection=threadLocal.get();
try {
if (connection != null && !connection.isClosed()) {
connection.close();
threadLocal.set(null);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs,Statement stmt){
DBCPConnectionManager.getInstance().closeAllResources(rs, stmt);
closeConnection();
}
}
2. 从DBCP连接池中拿到 Connection的工具类:
package com.pccw.test.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
public class DBCPConnectionManager {
private static DBCPConnectionManager dbcpConnection;
private static DataSource datasource;
//单例模式
private DBCPConnectionManager() {
datasource = initDataSource();
}
private static DataSource initDataSource() {
BasicDataSource ds = new BasicDataSource();
InputStream inputStream = null;
Properties p = new Properties();
String driverClassName = null;
String url = null;
String username = null;
String password = null;
int initialSize = 0;
int minIdle = 0;
int maxIdle = 0;
int maxWait = 0;
int maxActive = 0;
try {
inputStream = DBCPConnectionManager.class.getClassLoader()
.getResourceAsStream("dbcp.properties");
p.load(inputStream);
driverClassName = p.getProperty("driverClassName");
url = p.getProperty("url");
username = p.getProperty("username");
password = p.getProperty("password");
initialSize = Integer.parseInt(p.getProperty("initialSize"));
minIdle = Integer.parseInt(p.getProperty("minIdle"));
maxIdle = Integer.parseInt(p.getProperty("maxIdle"));
maxWait = Integer.parseInt(p.getProperty("maxWait"));
maxActive = Integer.parseInt(p.getProperty("maxActive"));
ds.setUrl(url);
ds.setDriverClassName(driverClassName);
ds.setUsername(username);
ds.setPassword(password);
ds.setInitialSize(initialSize); // 初始的连接数;
ds.setMaxActive(maxActive);
ds.setMinIdle(minIdle);
ds.setMaxIdle(maxIdle);
ds.setMaxWait(maxWait);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return ds;
}
public static synchronized DBCPConnectionManager getInstance() {
if (dbcpConnection == null) {
dbcpConnection = new DBCPConnectionManager();
}
return dbcpConnection;
}
public Connection getConnection() {
if (datasource != null) {
try {
Connection connection = datasource.getConnection();
return connection;
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
public void closeAllResources(ResultSet rs, Statement stmt) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3. dbcp.properties配置文件的内容:
driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://10.39.28.234:1433;DatabaseName=CRM_java
username=sa2
password=password_1234
initialSize=5
maxActive=50
maxIdle=15
minIdle=5
maxWait=28000
4.所需要的jar包:
commons-dbcp-1.4.jar
commons-pool-1.5.6.jar
SQLSERVER2005的驱动jar包
这是我在做一个项目中的写的访问数据库的工具类;虽然写好了,但是不知道,这个工具类在 同事调用时会不会出现什么问题,在这
里,我将其公布在网上,希望编程高手( java)臨临我的博客指教,本人在此表示衷心的感谢。 --------------------编程问答--------------------
ThreadLocal 和 DBCP连接池 --->共同管理Connection的 工具类代码,
补充:Java , Java EE