📄 dbconnectionpool.java
字号:
package com.tongtu.comm.sql;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2003-8-22
* Time: 14:26:19
* To change this template use Options | File Templates.
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;
/**
* JDBC数据库连接池类。<p>
*
* @author irevin
* @version 1.0
*/
public class DBConnectionPool {
//可用连接池
private Vector ConnectionPool = new Vector();
//最大连接数
private int maxConn;
//连接数量
private int connNumb;
//数据库参数
private String strURL;
private String strUser;
private String strPassword;
//连接池名
private String strName;
private final static int DEFAULT_MAX_CONNECTIONS = 30;
//
//private final static Logger log = Logger.getInstance("DBConnectionPool");
/**
* 构造方法。
*
* @param URL URL
* @param URL
*/
public DBConnectionPool(String URL){
this(URL,DEFAULT_MAX_CONNECTIONS);
}//end DBConnectionPool()
/**
* 构造方法。
*
* @param URL URL
* @param maxConn 最大连接数
*/
public DBConnectionPool(String URL, int maxConn){
this("", URL, null, null, maxConn);
}//end DBConnectionPool()
/**
* 构造方法。
*
* @param URL URL
* @param User 用户名
* @param Password 密码
* @param maxConn 最大连接数
*/
public DBConnectionPool(String URL, String User, String Password, int maxConn){
this("", URL, User, Password, maxConn);
}//end DBConnectionPool()
/**
* 构造方法。
*
* @param Name 连接池名
* @param URL URL
* @param User 用户名
* @param Password 密码
* @param maxConn 最大连接数
*/
public DBConnectionPool(String Name, String URL, String User, String Password, int maxConn){
this.strName = Name;
this.strURL = URL;
this.strUser = User;
this.strPassword = Password;
this.maxConn = maxConn;
}//end DBConnectionPool()
/**
* 释放数据库连接。
*
* @param conn 数据库连接对象
*/
public synchronized void freeConnection(Connection conn){
ConnectionPool.addElement(conn);
connNumb--;
notifyAll();
}//end freeConnection()
/**
* 取数据库连接。
*
* @return 数据库连接对象
* @throws SQLException SQL异常
*/
public synchronized Connection getConnection() throws SQLException{
//数据库连接对象
Connection conn = null;
//如果连接池中存在有数据库连接对象
while(conn==null)
{
if(ConnectionPool.size() > 0){
//从连接池中取第一个数据库连接对象
conn = (Connection) ConnectionPool.firstElement();
//从可用连接池中移出该连接
ConnectionPool.removeElementAt(0);
try{
//如果该连接已经关闭,则递归调用取连接方法
if(conn.isClosed()){
conn = getConnection();
}
} catch(SQLException e){
//出现异常,递归调用自己
conn = getConnection();
}//end try...catch...
} else if(maxConn == 0 || connNumb < maxConn){
//创建数据库连接
conn = newConnection();
}//end if...else if...
//如果数据库连接已经建立
if(conn==null)
{
try
{
wait(1000);
}
catch(Exception e)
{
}
}
}
if(conn != null){
//连接数加一
connNumb++;
}
//返回取得的数据库连接
return conn;
}//end getConnection()
/**
* 取数据库连接。
*
* @param timeOut 以毫秒计的等待时间限制
* @return 数据库连接对象
* @throws SQLException SQL异常
*/
public synchronized Connection getConnection(final long timeOut)
throws SQLException{
//起始时间
final long startTime = new Date().getTime();
//数据库连接
Connection conn = null;
//取数据库连接,如果超时则返回空
while((conn = getConnection()) == null){
try{
wait(timeOut);
} catch(InterruptedException e){
//log.error(e.getMessage());
}//end try...catch...
// if((new Date().getTime() - startTime) >= timeOut){
//超时返回空值
//log.error("getConnection error: Tiemout...");
// return null;
//}//end if
}//end while
return conn;
}//end getConnection()
/**
* 创建数据库连接。
*
* @return 数据库连接
*/
private Connection newConnection(){
Connection conn = null;
try{
//如果用户名为空,则使用URL取连接
if(null == this.strUser){
conn = DriverManager.getConnection(strURL);
} else{
//URL,用户名,密码
conn = DriverManager.getConnection(strURL, strUser, strPassword);
}//end if_else
//log...
//log.info("new Connection...");
} catch(SQLException e){
//error...
//log.error("newConncetion error: " + e.getMessage());
}//end try...catch...
return conn;
}//end newConnection()
/**
* 释放所有连接。
*/
public synchronized void release(){
//可用数据库连接中的连接对象集合
Enumeration allConnections = this.ConnectionPool.elements();
//逐个关闭
while(allConnections.hasMoreElements()){
//取下一个连接
final Connection conn;
conn = (Connection) allConnections.nextElement();
try{
//如果未关闭则关闭之
if(!conn.isClosed()){
conn.close();
}
//log.info("Connection released...");
} catch(SQLException e){
//异常记录
//log.error("dbpool release exception: " + e.getMessage());
}//end try...catch...
}//end while
//清空可用连接池
this.ConnectionPool.removeAllElements();
}//end release()
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -