📄 dbpool.java
字号:
/*
* Created on 2004-3-24
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package org.digitstore.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.digitstore.util.Config;
;
/**
* @author ming1
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class DBPool {
private static Hashtable pools = new Hashtable();
//单实例
// private static DBPool instance = null;
/** 最大连接数 */
private static int MAX_CONNECTION = 5;
/** 当前建立的连接数 */
private static int num_connections = 0;
// private int maxConnections = 15;
private Hashtable hashAvails = null;
private Hashtable hashBusys = null;
private long TIME_OUT = 0;
private String name = "";
private boolean inited = false;
//监控线程
private static ThreadMonitor monitor;
Log logger = LogFactory.getLog(this.getClass());
private DBPool(String name) {
hashAvails = new Hashtable();
hashBusys = new Hashtable();
MAX_CONNECTION = Integer.parseInt(Config.getProperty("database." + name
+ ".connectionmax", "5"));
TIME_OUT = Long.parseLong(Config.getProperty("database." + name
+ ".connectiontimeout", "10000"));
num_connections = 0;
this.name = name;
}
public static synchronized DBPool getInstance(String name) {
synchronized (DBPool.class) {
DBPool pool = (DBPool) pools.get(name);
if (pool == null) {
pool = new DBPool(name);
pools.put(name, pool);
monitor = new ThreadMonitor(pool);
monitor.start();
}
return pool;
}
}
/**
* 初始化
*
*/
private synchronized void init() {
if (inited) {
return;
}
synchronized (DBPool.class) {
logger.debug("connection pool init begin ");
hashAvails = new Hashtable();
hashBusys = new Hashtable();
try {
for (int i = 0; i < MAX_CONNECTION; i++) {
Connection con = newConnection(name);
hashAvails.put(con, Long.toString(System
.currentTimeMillis()));
}
} catch (SQLException e) {
logger.fatal("make new Connection fail!");
}
inited = true;
logger.info("connection pool init success!");
}
}
/**
* 清理死连接方法,有连接池监控线程调用
*
*/
protected void clean() {
synchronized (hashAvails) {
Enumeration e = hashAvails.keys();
while (e.hasMoreElements()) {
try {
Connection con = (Connection) e.nextElement();
if (con.isClosed()) {
hashAvails.remove(con);
con = newConnection(name);
hashAvails.put(con, Long.toString(System
.currentTimeMillis()));
}
} catch (Exception ex) {
logger.fatal("clean pool fail!");
}
}
logger.info("busy connection num:" + hashBusys.size());
logger.info("not busy connection num:" + hashAvails.size());
int total = hashAvails.size() + hashBusys.size();
for (int i = total; i < MAX_CONNECTION; i++) {
try {
hashAvails.put(newConnection(name), Long.toString(System
.currentTimeMillis()));
} catch (SQLException ex) {
logger.error("clean pool, add connection fail!");
}
}
}
}
/**
* 获得一个连接
*
* @param name
* @return
*/
public Connection getConnection() {
init();
//
// 没有空闲连接,进入等待
if (hashAvails.size() <= 0) {
try {
logger.info("waiting for connection ...");
Thread.sleep(TIME_OUT);
} catch (Exception e) {
}
}
Connection con = null;
synchronized (hashAvails) {
Enumeration e = hashAvails.keys();
if (e.hasMoreElements()) {
con = (Connection) e.nextElement();
hashAvails.remove(con);
}
try {
if (con == null || con.isClosed()) {
con = newConnection(name);
logger.fatal("connection error, rebuild a new connection");
}
} catch (SQLException ex) {
logger.fatal("rebuild a new connection fail");
}
}
synchronized (hashBusys) {
hashBusys.put(con, Long.toString(System.currentTimeMillis()));
logger.debug("get connection from pool");
}
return con;
}
/**
* 释放连接
*
* @param name
* @param connection
*/
public void free(Connection con) {
synchronized (hashAvails) {
synchronized (hashBusys) {
hashBusys.remove(con);
hashAvails.put(con, Long.toString(System.currentTimeMillis()));
}
}
//logger.debug("busy =" + hashBusys.size());
//logger.debug("avails =" + hashAvails.size());
}
/**
* 创建一个新的数据库连接
*/
private synchronized Connection newConnection(String name)
throws SQLException {
Connection conn = null;
try {
//logger.debug("try get a new Connection!");
String connectstring = Config.getProperty("database." + name
+ ".connectstring");
String connectdriver = Config.getProperty("database." + name
+ ".driver");
String user = Config.getProperty("database." + name + ".user");
String password = Config.getProperty("database." + name
+ ".password");
//Log.log(connectdriver);
Class.forName(connectdriver).newInstance();
if (user == null) {
conn = DriverManager.getConnection(connectstring, null);
} else {
conn = DriverManager.getConnection(connectstring, user,
password);
}
logger.info("NEW a Connection!");
return conn;
} catch (SQLException e) {
logger
.error(" public class DBCon private synchronized Connection newConnection() throws SQLException {");
throw e;
} catch (Exception e1) {
e1.printStackTrace();
logger.debug(e1);
return null;
}
}
public String getName() {
return name;
}
}
/**
* 连接池监控线程
*/
class ThreadMonitor extends Thread {
DBPool _instance;
Log logger = LogFactory.getLog(this.getClass());
ThreadMonitor(DBPool instance) {
_instance = instance;
}
public void run() {
while (true) {
try {
Thread.sleep(Long.parseLong(Config.getProperty("database."
+ _instance.getName() + ".cleaninterval", "3600000")));
_instance.clean();
} catch (Exception e) {
logger.error("pool monitor fail!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -