📄 jdbcpoolmanager.java
字号:
package com.icbcsdc.ddlexp.pub.connectionpool;
import java.sql.DriverPropertyInfo;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Hashtable;
import java.util.*;
import com.icbcsdc.ddlexp.pub.staticLog.Logger;
import com.icbcsdc.ddlexp.pub.util.ExceptionHandle;
/**
* @author zhangyc
* 管理类JDBCPoolManager支持对由属性文件定义的数据库连接
* 池的访问.客户程序可以调用getInstance()方法访问本类的唯一实例.
*/
public class JDBCPoolManager {
static private JDBCPoolManager instance=null; // 唯一实例
private Hashtable pools; //存储连接池对象
private HashSet drivers; //存储驱动名称
private Hashtable poolAttributes; //存储从配置文件中读取的信息
private String backDir="/usr/local/mysql";
private int poolMaxSize = 10;
private int poolMinSize = 4;
private int increaseStep = 1;
private int scanCount = 20;
private int scanInterval=60000;
private long getTimeout=2000;
private boolean debug=false;
/**
* 返回唯一实例.如果是第一次调用此方法,则创建实例
* @return JDBCPoolManager 唯一实例
*/
static synchronized public JDBCPoolManager getInstance() {
if (instance == null) {
instance = new JDBCPoolManager();
}
//clients++;
return instance;
}
/**
* 建构函数私有以防止其它对象创建本类实例
*/
private JDBCPoolManager() {
pools = new Hashtable();
drivers=new HashSet();
poolAttributes=new Hashtable();
}
/**
* 给定的连接池属性信息加载连接驱动并创建连接池实例.
* @Precondition 连接池规范不为空
* @Postcondition 加载连接驱动并创建连接池实例
* @param attr 连接池规范
* @exception 加载连接驱动或者创建连接池实例时出错
*/
private void addPool(PoolSpec attr)
throws JDBCException{
JDBCPool pool1=null;
poolAttributes.put(attr.poolName,attr);
try{
//判断该类驱动是否已经加载
boolean b=drivers.add(attr.driver);
//System.out.print(b);
if(b){
//加载jdbc驱动程序
Class.forName(attr.driver);
//System.out.println(attr.driver);
}
pool1=(JDBCPool)pools.get(attr.poolName);
if(pool1!=null) throw new JDBCException(JDBCCnnMgrConstants.SAME_POOL_EXIST);
pool1 =new JDBCPool(attr);
pools.put(attr.poolName,pool1);
}catch(Exception e){
e.printStackTrace();
String errMsg=ExceptionHandle.getMessage(e);
Logger.log("ERROR",JDBCCnnMgrConstants.EXCEP_ADD_POOL+" "+attr.poolName);
Logger.log("ERROR",errMsg);
throw new JDBCException(errMsg);
}
return;
}
/**
* 关闭所有连接池,撤销驱动程序的注册
* @param level 可以为JDBCPool.ABORT或JDBCPool.TRANSATIONAL两者之一
* @exception 关闭连接池时出错
*/
public synchronized void close()
throws JDBCException{
closeAll(JDBCPool.ABORT);
}
/**
* 关闭所有连接池,撤销驱动程序的注册
* @param level 可以为JDBCPool.ABORT或JDBCPool.TRANSATIONAL两者之一
* @exception 关闭连接池时出错
*/
private synchronized void closeAll(int level)
throws JDBCException {
Iterator iterator=poolAttributes.entrySet().iterator();
while(iterator.hasNext()){
PoolSpec attr=(PoolSpec)iterator.next();
JDBCPool pool=null;
pool=(JDBCPool)pools.get(attr.poolName);
if(pool!=null){
pool.shutdown(level);
}
}
poolAttributes.clear();
pools.clear();
}
/**
* 关闭某个连接池
* @param poolname 指定要关闭的连接池的名称
* @param level 可以为JDBCPool.ABORT或JDBCPool.TRANSATIONAL两者之一
* @exception 关闭连接池时出错
*/
private synchronized void close(String poolname,int level)
throws JDBCException {
JDBCPool pool=null;
pool=(JDBCPool)pools.get(poolname);
if(pool!=null){
pool.shutdown(level);
}
pools.remove(poolname);
}
/**
* 返回给由名字指定的连接池对象
* @param poolname 连接池对象
*/
private JDBCPool getPool(String poolname){
JDBCPool pool=null;
pool=(JDBCPool)pools.get(poolname);
return pool;
}
/**
* 返回所有连接池的名称
* @return String[] 返回所有连接池的名称
* */
private String[] getPoolNames(){
String[] poolnames;
int length=poolAttributes.size();
int i=0;
poolnames=new String[length];
Iterator iterator=poolAttributes.entrySet().iterator();
while(iterator.hasNext()){
PoolSpec attr=(PoolSpec)iterator.next();
poolnames[i]=attr.poolName;
i++;
}
return poolnames;
}
/**
* 获取空闲连接
* @param driver 连接驱动名称
* @param url 连接字符串,用于定位数据库
* @param user 提供数据库验证的用户
* @param passwd 提供数据库验证的密码
* */
public JDBCPool getJDBCPool(String driver,String url,String user,String passwd)
throws JDBCException{
JDBCCnn cnn=null;
String poolID=driver.trim()+url.trim()+"."+user.trim()+"."+passwd.trim();
JDBCPool pool=this.getPool(poolID);
if(pool==null){
PoolSpec attr=new PoolSpec(poolID,driver,url,user,passwd);
addPool(attr);
pool=this.getPool(poolID);
}
return pool;
}
/**
* 获取空闲连接
* @param driver 连接驱动名称
* @param url 连接字符串,用于定位数据库
* @param user 提供数据库验证的用户
* @param passwd 提供数据库验证的密码
public JDBCCnn getConnection(String driver,String url,String user,String passwd)
throws JDBCException{
JDBCCnn cnn=null;
String poolID=driver.trim()+url.trim()+"."+user.trim()+"."+passwd.trim();
JDBCPool pool=this.getPool(poolID);
if(pool==null){
PoolSpec attr=new PoolSpec(poolID,driver,url,user,passwd);
addPool(attr);
pool=this.getPool(poolID);
}
if(pool!=null) cnn=pool.getFreeConnection(this.getTimeout);
return cnn;
}
* */
/**
* 获取空闲连接
* @param driver 连接驱动名称
* @param url 连接字符串,用于定位数据库
* @param user 提供数据库验证的用户
* @param passwd 提供数据库验证的密码
* @param timeout 连接超时限制
* */
public JDBCPool getJDBCPool(String driver,String url,String user,String passwd,long timeout)
throws JDBCException{
JDBCCnn cnn=null;
String poolID=driver.trim()+url.trim()+"."+user.trim()+"."+passwd.trim();
JDBCPool pool=this.getPool(poolID);
if(pool==null){
PoolSpec attr=new PoolSpec(poolID,driver,url,user,passwd);
addPool(attr);
pool=this.getPool(poolID);
}
return pool;
}
/**
* 获取空闲连接
* @param driver 连接驱动名称
* @param url 连接字符串,用于定位数据库
* @param user 提供数据库验证的用户
* @param passwd 提供数据库验证的密码
* @param timeout 连接超时限制
public JDBCCnn getConnection(String driver,String url,String user,String passwd,long timeout)
throws JDBCException{
JDBCCnn cnn=null;
String poolID=driver.trim()+url.trim()+"."+user.trim()+"."+passwd.trim();
JDBCPool pool=this.getPool(poolID);
if(pool==null){
PoolSpec attr=new PoolSpec(poolID,driver,url,user,passwd);
addPool(attr);
pool=this.getPool(poolID);
}
if(pool!=null) cnn=pool.getFreeConnection(timeout);
return cnn;
}
* */
/**
* 判断数据库是否可以连通
* @param driver 连接驱动名称
* @param url 连接字符串,用于定位数据库
* @param user 提供数据库验证的用户
* @param passwd 提供数据库验证的密码
* */
public boolean canConnect(String driver,String url,String user,String passwd)
throws JDBCException
{
boolean result=false;
String poolID=driver.trim()+url.trim()+"."+user.trim()+"."+passwd.trim();
JDBCPool pool=this.getPool(poolID);
if(pool==null){
PoolSpec attr=new PoolSpec(poolID,driver,url,user,passwd);
attr.setBackDir(this.backDir);
attr.setIncreaseStep(this.increaseStep);
attr.setPoolMaxSize(this.poolMaxSize);
attr.setPoolMinSize(this.poolMinSize);
attr.setScanCount(this.scanCount);
attr.setScanInterval(this.scanInterval);
addPool(attr);
pool=this.getPool(poolID);
}
if(pool!=null) result=pool.hasConnect();
else result=false;
return result;
}
/**
* 释放连接,每个线程在使用完连接对象后都应该主动的释放连接
* 连接释放后可以被其他线程重复使用
* @param cnn 要释放的连接对象,
* */
public void ReleaseConnection(JDBCCnn cnn)
throws JDBCException{
JDBCPool pool=this.getPool(cnn.parent);
pool.releaseConnection(cnn);
}
/**
* 关闭一个连接对象,连接对象关闭后不再可用
* @param cnn 要关闭的连接对象
* */
public void closeConnectin(JDBCCnn cnn)
throws JDBCException{
JDBCPool pool=this.getPool(cnn.parent);
pool.closeConnectin(cnn);
}
public static void main(String[] args) {
/**
String driverClassName = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String dbUser = "root";
String dbPassword = "root";
int id=0;
String backDir="/usr/local/mysql";
boolean notStop=true;
int count=25;
JDBCCnn jcnn=null;
String sqlstr="select * from t1";
String sqlstr1="select * from t2";
JDBCPoolManager manager=JDBCPoolManager.getInstance();
try {
jcnn=manager.getJDBCPool(driverClassName,url,dbUser,dbPassword);
ResultSet rs=jcnn.executeQuery(sqlstr);
while(rs.next()){
System.out.println(rs.getInt("col1"));
}
Thread.currentThread().sleep(1000);
ResultSet rs1=jcnn.executeQuery(sqlstr1);
ResultSetMetaData md= rs1.getMetaData();
String colname=md.getColumnName(1);
System.out.println(colname);
while(rs1.next()){
System.out.println(rs1.getInt("c1"));
}
} catch (Exception e) {
System.out.println(ExceptionHandle.getMessage(e));
//e.printStackTrace();
}
**/
}
/**
* Returns the backDir.
* @return String
*/
public String getBackDir() {
return backDir;
}
/**
* Returns the increaseStep.
* @return int
*/
public int getIncreaseStep() {
return increaseStep;
}
/**
* Returns the poolMaxSize.
* @return int
*/
public int getPoolMaxSize() {
return poolMaxSize;
}
/**
* Returns the poolMinSize.
* @return int
*/
public int getPoolMinSize() {
return poolMinSize;
}
/**
* Returns the scanCount.
* @return int
*/
public int getScanCount() {
return scanCount;
}
/**
* Returns the scanInterval.
* @return int
*/
public int getScanInterval() {
return scanInterval;
}
/**
* Sets the backDir.
* @param backDir The backDir to set
*/
public void setBackDir(String backDir) {
this.backDir = backDir;
}
/**
* Sets the increaseStep.
* @param increaseStep The increaseStep to set
*/
public void setIncreaseStep(int increaseStep) {
this.increaseStep = increaseStep;
}
/**
* Sets the poolMaxSize.
* @param poolMaxSize The poolMaxSize to set
*/
public void setPoolMaxSize(int poolMaxSize) {
this.poolMaxSize = poolMaxSize;
}
/**
* Sets the poolMinSize.
* @param poolMinSize The poolMinSize to set
*/
public void setPoolMinSize(int poolMinSize) {
this.poolMinSize = poolMinSize;
}
/**
* Sets the scanCount.
* @param scanCount The scanCount to set
*/
public void setScanCount(int scanCount) {
this.scanCount = scanCount;
}
/**
* Sets the scanInterval.
* @param scanInterval The scanInterval to set
*/
public void setScanInterval(int scanInterval) {
this.scanInterval = scanInterval;
}
/**
* Returns the getTimeout.
* @return long
*/
public long getGetTimeout() {
return getTimeout;
}
/**
* Sets the getTimeout.
* @param getTimeout The getTimeout to set
*/
public void setGetTimeout(long getTimeout) {
this.getTimeout = getTimeout;
}
/**
* Returns the debug.
* @return boolean
*/
public boolean isDebug() {
return debug;
}
/**
* Sets the debug.
* @param debug The debug to set
*/
public void setDebug(boolean debug) {
this.debug = debug;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -