📄 dbconnectionpool.java
字号:
/*
* Created on 2005-11-15
* Author 曹汕
* Version 1.0
* Copyright by CS.SSPKU Inc. All rights reserved.
*/
package com.struts.utils;
import java.sql.*;
import java.util.Enumeration;
import java.util.Vector;
import org.apache.commons.logging.*;
/**
* @author cs
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class DBConnectionPool {
private String password;
private String URL;
private String user;
private int maxConn;
private int checkedOut;
private Vector freeConnections=new Vector();
private Log log=LogFactory.getLog(this.getClass().getName());
//构造函数取得上述的所有参数:
public DBConnectionPool( String URL, String user,String password, int maxConn) {
this.URL = URL;
this.user = user;
this.password = password;
this.maxConn = maxConn;
}
/**
* 获取数据库连接
*/
public synchronized Connection getConnection() {
Connection con = null;
//存在空闲连接,则从空闲的连接中获取
if (freeConnections.size() > 0) {
//Pick the first Connection in the Vector to get round-robin usage
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try {
if (con.isClosed()) {
log.info("Removed bad connection from the connection pool" );
//Try again recursively
con = getConnection();
}
}
catch (SQLException e) {
log.info("Removed bad connection from the connection pool",e);
//Try again recursively
con = getConnection();
}
}
//最大连接数为0或者已使用连接数小于最大连接数,新创建一个连接
else if (maxConn == 0 || checkedOut < maxConn) {
con = newConnection();
}
if (con != null) {
checkedOut++;
}
return con;
}
/**
* 创建一个数据库连接
*/
private Connection newConnection() {
Connection con = null;
try {
if (user == null) {
con = DriverManager.getConnection(URL);
}
else {
con = DriverManager.getConnection(URL, user, password);
}
log.info("Created a new connection in pool ");
}
catch (SQLException e) {
log.info( "Can not create a new connection for " + URL,e);
return null;
}
return con;
}
// public synchronized Connection getConnection(long timeout) {
//
// long startTime = new Date().getTime();
// Connection con;
// while ((con = getConnection()) == null) {
// try {
// wait(timeout);
// }
// catch (InterruptedException e) {}
// if ((new Date().getTime() - startTime) >= timeout) {
// // Timeout has expired
// return null;
// }
// }
// return con;
// }
/**
* 将空闲的连接放入空闲连接vector中
* @param con
*/
public synchronized void freeConnection(Connection con) {
// Put the connection at the end of the Vector
freeConnections.addElement(con);
checkedOut--;
notifyAll();
}
/**
* 释放连接
*/
public synchronized void release() {
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
log.info("Closed connection for pool " );
}
catch (SQLException e) {
log.info("Can not close connection for pool ",e);
}
}
freeConnections.removeAllElements();
}
// /**
// * 日志记录
// * @param e
// * @param string
// * @throws Exception
// */
// private void log(SQLException e, String log) {
// e.printStackTrace();
// System.out.println(log);
// }
// /**
// * @param string
// */
// private void log(String log) {
// System.out.println(log);
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -