📄 dbconnectionmanager.java
字号:
* @param props The connection pool properties
*/
private void loadDrivers(Properties props) {
String driverClasses = props.getProperty("drivers");
StringTokenizer st = new StringTokenizer(driverClasses);
while (st.hasMoreElements()) {
String driverClassName = st.nextToken().trim();
try {
Driver driver = (Driver)
Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver);
Log.printEvent("Registered JDBC driver " + driverClassName, logFile);
}
catch (Exception e) {
Log.printEvent("Can't register JDBC driver: " +
driverClassName + ", Exception: " + e, logFile);
}
}
}
/**
* This inner class represents a connection pool. It creates new
* connections on demand, up to a max number if specified.
* It also makes sure a connection is still open before it is
* returned to a client.
*/
class DBConnectionPool {
private int checkedOut; //已用连接数
private Vector freeConnections = new Vector(); //可用连接数
private int maxConn; //最大连接数
private int minConn; //最小连接数
private int defaultConn; //默认连接数
private String name; //数据库名称
private String password; //登陆密码
private String URL; //数据库路径
private String user; //用户名称
public int getUsedConNum() {
return checkedOut;
}
public int getFreeConNum() {
return freeConnections.size();
}
/**
* Creates new connection pool.
*
* @param name The pool name
* @param URL The JDBC URL for the database
* @param user The database user, or null
* @param password The database user password, or null
* @param maxConn The maximal number of connections, or 0
* for no limit
*/
public DBConnectionPool(String name, String URL, String user,
String password,
int maxConn, int minConn, int defaultConn) {
this.name = name;
this.URL = URL;
this.user = user;
this.password = password;
this.maxConn = maxConn;
this.minConn = minConn;
this.defaultConn = defaultConn;
initConnections();
}
/**
* Checks in a connection to the pool. Notify other Threads that
* may be waiting for a connection.
*
* @param con The connection to check in
*/
public synchronized void freeConnection(Connection con) {
// Put the connection at the end of the Vector
int free = freeConnections.size();
if (con == null || ( (checkedOut + free > maxConn) && (free >= minConn))) {
try {
if (con != null) {
con.close();
Log.printEvent("Closed connection for pool " + name, logFile);
}
else
Log.printEvent("Thread Name :" + Thread.currentThread().getName() +
" Closed Connecion is null " + name, logFile);
}
catch (SQLException e) {
Log.printError(e,"Can't close connection for pool " + name , logFile);
}
}
else {
Statement stmt = null;
try {
stmt = con.createStatement();
stmt.close();
freeConnections.addElement(con);
}
catch (Exception e) {
if (stmt != null) {
try {
stmt.close();
con.close();
}
catch (SQLException ex) {}
}
}
}
checkedOut--;
notifyAll();
}
/**
* Checks out a connection from the pool. If no free connection
* is available, a new connection is created unless the max
* number of connections has been reached. If a free connection
* has been closed by the database, it's removed from the pool
* and this method is called again recursively.
*/
public synchronized java.sql.Connection getConnection() {
java.sql.Connection con = null;
con = getConnection_dg();
if (con != null) {
checkedOut++;
}
return con;
}
private java.sql.Connection getConnection_dg() {
java.sql.Connection con = null;
if (freeConnections.size() > 0) {
con = (java.sql.Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
Statement stmt = null;
try {
if (con == null || con.isClosed()) {
Log.printEvent("Removed bad connection from 1 in:" +
Thread.currentThread().getName(), logFile);
con = getConnection_dg();
}
else {
stmt = con.createStatement();
stmt.close();
}
}
catch (SQLException e) {
Log.printError(e,"Removed bad connection from 2 in:" +
Thread.currentThread().getName(), logFile);
if (stmt != null) {
try {
stmt.close();
con.close();
}
catch (SQLException ex) {}
}
con = getConnection_dg();
}
}
else if (maxConn == 0 || checkedOut < maxConn) { //如果maxCon为零则可无限增大
con = newConnection();
}
return con;
}
/**
* Checks out a connection from the pool. If no free connection
* is available, a new connection is created unless the max
* number of connections has been reached. If a free connection
* has been closed by the database, it's removed from the pool
* and this method is called again recursively.
* <P>
* If no connection is available and the max number has been
* reached, this method waits the specified time for one to be
* checked in.
*
* @param timeout The timeout value in milliseconds
*/
public synchronized java.sql.Connection getConnection(long timeout) {
long startTime = new Date().getTime();
java.sql.Connection con;
while ( (con = getConnection()) == null) {
try {
wait(timeout); //有notifyAll()唤醒或时间到自动唤醒
}
catch (InterruptedException e) {}
if ( (new Date().getTime() - startTime) >= timeout) {
// Timeout has expired
return null;
}
}
return con;
}
/**
* Closes all available connections.
*/
public synchronized void release() {
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
java.sql.Connection con = (java.sql.Connection) allConnections.
nextElement();
try {
con.close();
Log.printEvent("Closed connection for pool " + name, logFile);
}
catch (SQLException e) {
Log.printEvent("Can't close connection for pool " + name +
" Exception :" + e, logFile);
}
}
freeConnections.removeAllElements();
}
/**
* Creates a new connection, using a userid and password
* if specified.
*/
private java.sql.Connection newConnection() {
java.sql.Connection con = null;
try {
if (user == null) {
con = DriverManager.getConnection(URL);
}
else {
con = DriverManager.getConnection(URL, user, password);
}
Log.printEvent("Created a new connection in pool " + name, logFile);
}
catch (SQLException e) {
Log.printError(e,"Can't create a new connection for " + URL , logFile);
return null;
}
return con;
}
private void initConnections() {
java.sql.Connection con = null;
for (int i = 0; i < defaultConn; i++) {
con = newConnection();
freeConnections.addElement(con);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -