📄 dbconnectionmanager.java
字号:
package addressbook.model;
/**
* <p>Title: 公共类</p>
* <p>Description: 公共类---数据库连接</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: fxm</p>
* @author liaohh
* @version 2.0
* @todo:
*/
import java.sql.*;
import java.util.Vector;
import java.util.Enumeration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
class DBConnectionManager {
private static Log log = LogFactory.getLog(DBConnectionManager.class);
private static final int TIME_BETWEEN_RETRIES = 500; // O.5 second
static private DBConnectionManager instance = null;
private DBConnectionPool pool = null;
private DBConnectionManager(DBOptions option) {
try {
Class.forName(option.driverClassName).newInstance();
} catch (Exception e) {
log.fatal("DBConnectionManager: 不能加载驱动 = " + option.driverClassName);
}
//if (pool == null) {//uncomment since pool is an instance variable
pool = new DBConnectionPool(option.databaseURL, option.databaseUser, option.databasePassword, option.maxConnection);
//}
}
/**
* 当这个方法第一次被调用的时候得到一个单独的实例
*
* @return DBConnectionManager The single instance.
*/
static synchronized public DBConnectionManager getInstance() {
if (instance == null) {
DBOptions option = new DBOptions();
instance = new DBConnectionManager(option);
}
return instance;
}
/**
* 当这个方法第一次被调用的时候得到一个单独的实例
*
* @return DBConnectionManager The single instance.
*/
static synchronized public DBConnectionManager getInstance(DBOptions option) {
if (instance == null) {
if (option == null) {
option = new DBOptions();
}
instance = new DBConnectionManager(option);
}
return instance;
}
/**
* 从数据库连接池中得到一个连接
*
* @param con The Connection
*/
void freeConnection(Connection con) {
pool.freeConnection(con);
}
/**
* 当数据库连接池中定义的最大连接数还没有达到的时候,创建一个连接
*
* @return Connection The connection or null
*/
Connection getConnection() {
return pool.getConnection();
}
/**
* 如果没有可用的连接,而且连接数没有达到最大怎创建一个连接,
* 如果连接数达到最大则,等待连接释放后在创建新的连接
* @param time The number of milliseconds to wait
* @return Connection The connection or null
*/
Connection getConnection(long time) {
return pool.getConnection(time);
}
/**
* 关闭所有连接
* @return true 如果数据库连接池是空的或者有可以使用的连接
* false 如果连接数超过连接池允许的连接数
*/
boolean release() {
return pool.release();
}
class DBConnectionPool {
private int checkedOut = 0;
private Vector freeConnections = new Vector();
private int maxConn = 0;
private String password = null;
private String URL = null;
private String user = null;
/**
* 创建一个新的连接池
* @param URL JDBC 驱动的地址
* @param user 数据库用户名
* @param password 数据库密码
* @param maxConn 最大的连接数
*/
public DBConnectionPool(String URL, String user, String password, int maxConn) {
this.URL = URL;
this.user = user;
this.password = password;
this.maxConn = maxConn;
}
synchronized void freeConnection(Connection con) {
if (con != null) {
if (checkedOut <= 0) {
// 这个表示连接数超过最大的连接数
try {
log.debug("DBConnectionManager: 关闭连接");
con.close();
} catch (SQLException ex) { }
} else {
freeConnections.addElement(con);
checkedOut--;
notifyAll();
}
}
}
synchronized Connection getConnection() {
Connection con = null;
while ( (freeConnections.size() > 0) && (con == null) ) {
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try {
if (con.isClosed()) {
log.info("删除坏连接!");
con = null;
}
} catch (SQLException e) {
con = null;
}
}
if (con == null) {
if (maxConn == 0 || checkedOut < maxConn) {
con = newConnection();
}
}
if (con != null) {
checkedOut++;
}
return con;
}
Connection getConnection(long timeout) {
long startTime = System.currentTimeMillis();
Connection con;
while ((con = getConnection()) == null) {
long elapsedTime = System.currentTimeMillis() - startTime;
if (elapsedTime >= timeout) {
return null;
}
long timeToWait = timeout - elapsedTime;
if (timeToWait > TIME_BETWEEN_RETRIES) timeToWait = TIME_BETWEEN_RETRIES;// we dont want to wait for more than TIME_BETWEEN_RETRIES second each time
try {
Thread.sleep(timeToWait);
} catch (InterruptedException e) {}
}
return con;
}
synchronized boolean release() {
boolean retValue = true;
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
} catch (SQLException e) {
log.error("不能关闭连接!");
}
}
freeConnections.removeAllElements();
if (checkedOut != 0) {
retValue = false;
log.warn("DBConnectionManager: 内置的连接池没有正确配置好");
}
checkedOut = 0;
return retValue;
}
private Connection newConnection() {
Connection con = null;
try {
if (user == null) {
con = DriverManager.getConnection(URL);
} else {
con = DriverManager.getConnection(URL, user, password);
}
con.setAutoCommit(true);//thread 804 by trulore
} catch (SQLException e) {
log.error("不能在连接池中创建新的连接. URL = " + URL, e);
return null;
}
return con;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -