jdbc连接池

来源:互联网 发布:潜伏结局 知乎 编辑:程序博客网 时间:2024/06/06 10:02

public class ConnectionPool {

private static Set m_notUsedConnection = new HashSet();//空闲的连接
private static LinkedList m_usedUsedConnection = new LinkedList();//正在使用的连接
private static String m_url = "jdbc:mysql://localhost:3306/userdata?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true";//远程服务器部署的连接
private static String m_user = "root";//服务器数据库账号
private static String m_password = "root";服务器数据库密码
static private long m_lastClearClosedConnection = System
.currentTimeMillis();//最后一次清理的时间


public static long CHECK_CLOSED_CONNECTION_TIME = 6*60*60*1000;//设置清理连接的时间间隔


static {
initDriver();
}


private ConnectionPool() {
}


//加载驱动
private static void initDriver() {
Driver driver = null;
try {
driver = (Driver) Class.forName("com.mysql.jdbc.Driver")
.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
/**
* try { driver = (Driver)
* Class.forName("org.postgresql.Driver").newInstance(); } catch
* (Exception e) { e.printStackTrace(); }
*/
}

public static synchronized Connection getConnection() {
//每次获取连接的时候检查是否该清理连接
clearClosedConnection();
Connection conn =null;
//如果空闲连接的个数大于0,那么从连接池直接获取,不在新创建连接
if(m_notUsedConnection.size()>0){
Iterator iterator = m_notUsedConnection.iterator();
while (iterator.hasNext()) {
conn =(Connection)m_notUsedConnection.iterator().next();
try {
if (conn.isClosed()) {
m_notUsedConnection.remove(conn);
continue;
}
m_usedUsedConnection.add(conn);
m_notUsedConnection.remove(conn);
return conn;
} catch (Exception e) {
// e.printStackTrace();
}
}
}else{
conn=getNewConnection();
m_usedUsedConnection.add(conn);
}
return conn;
}
//新获取连接
private static Connection getNewConnection() {


try {
Connection con = DriverManager.getConnection(m_url, m_user,
m_password);
return con;
} catch (Exception e) {
}


return null;
}
//将用完后的连接还回连接池
public static synchronized void close(Connection conn) {

try {
if(m_usedUsedConnection.contains(conn)){
m_usedUsedConnection.remove(conn);
}
if(!conn.isClosed()){
m_notUsedConnection.add(conn);
}
} catch (Exception e) {
// TODO: handle exception
}

}
//清理连接
private static void clearClosedConnection() {
long time = System.currentTimeMillis();
如果当前时间小于最后一次清理时间则重新获取最后一次清理时间
if (time < m_lastClearClosedConnection) {
time = m_lastClearClosedConnection;
return;
}
//如果当前时间距离最后一次清理时间大于设置的时间间隔,那么清理
if (time - m_lastClearClosedConnection < CHECK_CLOSED_CONNECTION_TIME) {
return;
}
m_lastClearClosedConnection = time;



Iterator iterator = m_notUsedConnection.iterator();
//检查关闭的连接从连接池中清除
while (iterator.hasNext()) {
try {
Connection cc=(Connection)iterator.next();
if (cc.isClosed()) {
m_notUsedConnection.remove(cc);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
m_notUsedConnection.remove((Connection)iterator.next());
}
}
int decrease = getDecreasingConnectionCount();
//如果空闲的连接数大于当前总连接数的1/3,那么清理1/3的连接
if (m_notUsedConnection.size() < decrease) {
return;
}

while (decrease-- > 0) {
try {
Connection cc=(Connection)m_notUsedConnection.iterator().next();
cc.close();
m_notUsedConnection.remove(cc);
} catch (Exception e) {
e.printStackTrace();
}
}
}

public static int getDecreasingConnectionCount() {
int count = 0;
int current = getConnectionCount();
if (current < 10) {
return 0;
}


return current / 3;
}

public static int getConnectionCount() {
return m_notUsedConnection.size() + m_usedUsedConnection.size();
}


public static int getUsedConnectionCount() {
return m_usedUsedConnection.size();

}


public static int getNotUsedConnection() {
return m_notUsedConnection.size();
}

public synchronized static void printDebugMsg() {
printDebugMsg(System.out);
}


public synchronized static void printDebugMsg(PrintStream out) {
StringBuffer msg = new StringBuffer();
msg.append("debug message in" + ConnectionPool.class.getName());
msg.append("\r\n");
msg.append("total count is connection pool:" + getConnectionCount());
msg.append("\r\n");
msg.append("not used connection count:" + getNotUsedConnection());
msg.append("\r\n");
msg.append("used connection,count:" + getUsedConnectionCount());
out.println(msg);
out.println();
}
}