📄 connectionpool.java
字号:
package com.hualong.database.JDBC;
/**
* Created by IntelliJ IDEA.
* User:
* Date: 2004-01-27
* Time: 12:50:36
* 模块功能描述:用来维护和管理数据库连接的连接池管理类。
*/
import com.hualong.application.ApplicationContext;
import com.hualong.database.scheduler.Scheduler;
import com.hualong.tools.MyTime;
import com.hualong.UI.WndMain;
import java.io.PrintWriter;
import java.sql.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
public final class ConnectionPool implements Runnable {
/**
* 数据库连接源名称
*/
private String strDataSource;
/**
* Oracle数据库主机IP地址
*/
private String strOracleHost;
/**
* Oracle数据库的端口
*/
private String strOraclePort;
/**
* Oracle数据库服务ID
*/
private String strOracleServiceID;
/**
* JDBC 驱动名称
*/
private String strDriverName;
/**
* JDBC 连接的URL
*/
private String strConnectionURL;
/**
* JDBC连接的用户名称
*/
private String strUserName;
/**
* JDBC-OBBC DSN参数
*/
private String strDSN;
/**
* JDBC连接的口令
*/
private String strPassword;
/**
* 连接池的最小连接个数
*/
private int nConnectionPoolSize;
/**
* 连接池的最大连接个数
*/
private int nConnectionPoolMax;
/**
* 单个连接的最大使用个数
*/
private int nConnectionUseCount;
/**
* 连接的最大空闲时间(分钟)
*/
private int nConnectionTimeout;
/**
* JDBC连接属性
*/
//private Properties JDBCProperties;
/**
* 数据库对象连接池,存储ConnectionObject对象
*/
private Vector pool;
/**
* 保存JDBC驱动的并发连接的最大个数
*/
private int nMaxConnections = -1;
/**
* 连接调度对象
*/
private Scheduler scheduler;
/**
* 连接超时时间
*/
public static int TIMEOUT_MS = 20000;
/**
* 初始化数据库连接参数,同时初始化连接池对象
*
* @return 如果数据库连接池初始化成功则返回true
*/
public boolean initialize() {
//设置数据库连接信息
strDataSource = ApplicationContext.getDatabaseConfig().getDataSource();
if (strDataSource == null || strDataSource.length() == 0)
return false;
if (strDataSource.equals("Oracle")) {
this.strOracleHost = ApplicationContext.getDatabaseConfig().getOracleHost();
this.strOraclePort = ApplicationContext.getDatabaseConfig().getOraclePort();
this.strOracleServiceID = ApplicationContext.getDatabaseConfig().getOracleServiceID();
this.strUserName = ApplicationContext.getDatabaseConfig().getOracleUser();
this.strPassword = ApplicationContext.getDatabaseConfig().getOraclePassword();
} else if (strDataSource.equals("PointBase")) {
this.strDriverName = ApplicationContext.getDatabaseConfig().getPointBaseDriver();
this.strConnectionURL = ApplicationContext.getDatabaseConfig().getPointBaseURL();
this.strUserName = ApplicationContext.getDatabaseConfig().getPointBaseUserName();
this.strPassword = ApplicationContext.getDatabaseConfig().getPointBasePassword();
} else if (strDataSource.equals("sun_odbc_jdbc")) {
this.strDriverName = ApplicationContext.getDatabaseConfig().getOdbcDriver();
this.strDSN = ApplicationContext.getDatabaseConfig().getOdbcDSN();
this.strUserName = ApplicationContext.getDatabaseConfig().getOdbcUser();
this.strPassword = ApplicationContext.getDatabaseConfig().getOdbcPassword();
}
//设置数据库连接池信息
this.nConnectionPoolSize = ApplicationContext.getDatabaseConfig().getConnectionPoolSize();
this.nConnectionPoolMax = ApplicationContext.getDatabaseConfig().getConnectionPoolMaxSize();
this.nConnectionUseCount = ApplicationContext.getDatabaseConfig().getConnectionUseCount();
this.nConnectionTimeout = ApplicationContext.getDatabaseConfig().getConnectionTimeout();
try {
//创建数据库连接池
createPool();
// Start our timer so we can timeout connections.
scheduler = new Scheduler();
scheduler.schedule(this, TIMEOUT_MS);
} catch (Exception e) {
//e.printStackTrace();
WndMain.displayInformation("数据库连接",
"数据库连接失败,原因分析可能是数据库服务未启动!");
return false;
}
return true;
}
/**
* 销毁连接池中的内容,关闭已打开数据库连接对象,并释放所有资源
* Modify by zfs 2004.01.27
*/
public void destroy() {
ConnectionObject co = null;
try {
// Clear our pool
if (pool != null) {
// Loop throught the pool and close each connection
for (int i = 0; i < pool.size(); i++) {
co = (ConnectionObject) pool.elementAt(i);
System.out.println("Destroy : " + co.toString());
close(co);
}
}
pool = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 从连接池中获取一个可用的JDBC连接,如果需要则创建新的.
* 直到配置配置文件指定的最大连接个数.
*
* @return 返回JDBC最大连接个数,如果超过最大连接个数则返回null
*/
public synchronized Connection getConnection() {
// If there is no pool it must have been destroyed
if (pool == null) {
return null;
}
Connection con = null;
ConnectionObject connectionObject = null;
int nPoolSize = pool.size();
// Get the next available connection
for (int i = 0; i < nPoolSize; i++) {
// Get the ConnectionObject from the pool
ConnectionObject co = (ConnectionObject) pool.elementAt(i);
// If this is a valid connection and it is not in use,
// grab it
if (co.isAvailable()) {
connectionObject = co;
break;
}
}
// No more available connections. If we aren't at the
// maximum number of connections, create a new entry
// in the pool
if (connectionObject == null) {
if ((nConnectionPoolMax < 0) ||
((nConnectionPoolMax > 0) &&
(nPoolSize < nConnectionPoolMax))) {
// Add a new connection.
int i = addConnection();
// If a new connection was created, use it
if (i >= 0) {
connectionObject = (ConnectionObject) pool.elementAt(i);
}
} else {
trace("Maximum number of connections exceeded");
}
}
// If we have a connection, set the last time accessed,
// the use count, and the in use flag
if (connectionObject != null) {
connectionObject.bInUse = true;
connectionObject.useCount++;
touch(connectionObject);
con = connectionObject.con;
}
return con;
}
/**
* 关闭指定的连接,如果使用次数超过最大使用次数.则从
* 连接池中删除
*
* @param con 被关闭的连接
*/
public synchronized void close(Connection con) {
// Find the connection in the pool
int nIndex = find(con);
if (nIndex != -1) {
ConnectionObject co = (ConnectionObject) pool.elementAt(nIndex);
// If the use count exceeds the max, remove it from
// the pool.
if ((this.nConnectionUseCount > 0) && (co.useCount >= this.nConnectionUseCount)) {
trace("Connection use count exceeded");
removeFromPool(nIndex);
} else {
// Clear the use count and reset the time last used
touch(co);
co.bInUse = false;
}
}
}
/**
* 设置当前连接为可使用状态
*
* @param con 数据库连接
*/
public synchronized void setAvailable(Connection con) {
int nIndex = find(con);
if (nIndex != -1) {
ConnectionObject co = (ConnectionObject) pool.elementAt(nIndex);
co.bInUse = false;
}
}
/**
* 打印连接池内的ConnectionObject对象的内容信息
*/
public void printPool() {
//printPool(new PrintWriter(System.out));
System.out.println("--ConnectionPool--");
if (pool != null) {
for (int i = 0; i < pool.size(); i++) {
ConnectionObject co = (ConnectionObject) pool.elementAt(i);
System.out.println("" + i + "=" + co);
}
}
}
/**
* 打印连接池内的ConnectionObject对象的内容信息
*/
public void printPool(PrintWriter out) {
out.println("--ConnectionPool--");
if (pool != null) {
for (int i = 0; i < pool.size(); i++) {
ConnectionObject co = (ConnectionObject)
pool.elementAt(i);
out.println("" + i + "=" + co);
}
}
}
/**
* 返回连接池的ConnectionObject对象的枚举集合变量.
*/
public Enumeration getConnectionPoolObjects() {
return pool.elements();
}
/**
* 根据给定的索引号从连接池中删除ConnectionObject对象
*
* @param nIndex 索引号
*/
private synchronized void removeFromPool(int nIndex) {
// Make sure the pool and index are valid
if (pool != null) {
if (nIndex < pool.size()) {
// Get the ConnectionObject and close the connection
ConnectionObject co = (ConnectionObject)
pool.elementAt(nIndex);
close(co);
// Remove the element from the pool
pool.removeElementAt(nIndex);
}
}
}
/**
* 根据给定的ConnectionObject,这个数据库连接
*
* @param connectionObject ConnectionObject实例
*/
private void close(ConnectionObject connectionObject) {
if (connectionObject != null) {
if (connectionObject.con != null) {
try {
// Close the connection
connectionObject.con.close();
} catch (Exception ex) {
// Ignore any exceptions during close
}
// Clear the connection object reference
connectionObject.con = null;
}
}
}
/**
* 创建和初始化数据库连接池.
* 一个定时线程也被创建,用来处理连接超时.
*
* @ return 如果连接池被创建成功则返回true
*/
private void createPool() throws Exception {
// Sanity check our properties
/*
if (this.strDriverName == null && ) {
throw new Exception("JDBCDriver property not found");
}
if (strConnectionURL == null) {
throw new Exception("JDBCConnectionURL property not found");
}
if (nConnectionPoolSize < 0) {
throw new Exception("ConnectionPoolSize property not found");
}
if (nConnectionPoolSize == 0) {
throw new Exception("ConnectionPoolSize invalid");
}
if (nConnectionPoolMax < nConnectionPoolSize) {
trace("WARNING - ConnectionPoolMax is invalid and will " +
"be ignored");
nConnectionPoolMax = -1;
}
if (nConnectionTimeout < 0) {
// Set the default to 30 minutes
nConnectionTimeout = 30;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -