/*
* class : 连接池操作类
* author : 庄伟斌
* version: V1.0
* updatelog:
* */
package com.landraydev.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
public class DBConnectionPool {
private int inUsed = 0;
private ArrayList freeConnections = new ArrayList();
private int minConn;
private int maxConn;
private String poolname;
private String dbpassword;
private String dbuser;
private String dbdriver;
private String dburl;
public DBConnectionPool() {
// TODO Auto-generated constructor stub
}
/*
* 创建资源池
*/
public DBConnectionPool(String poolname, String dbdriver, String dburl,
String dbuser, String dbpassword, int maxconn) {
this.setPoolname(poolname);
this.setDbdriver(dbdriver);
this.setDburl(dburl);
this.setDbuser(dbuser);
this.setDbpassword(dbpassword);
this.setMaxConn(maxconn);
}
/*
* 获取链接池中数据库连接资源 注释:获取链接资源,如果没有空闲链接资源,新增资源
*/
public synchronized Connection getConnection(long timeout) {
Connection con = null;
long startTime = new Date().getTime();
while ((con = getConnection()) == null) {
try {
wait(timeout);
} catch (java.lang.InterruptedException e) {
e.printStackTrace();
}
if ((new Date().getTime() - startTime >= timeout)) {
return null;
}
}
return con;
}
/*
* 获取链接池中数据库连接资源注释:获取链接资源,如果没有空闲链接资源,新增资源
*/
public synchronized Connection getConnection() {
Connection con = null;
// 连接分配
if (this.freeConnections.size() > 0) {
con = (Connection) this.freeConnections.get(0);
this.freeConnections.remove(0);
try {
if ((con == null) || (con.isClosed()))
con = getConnection();
} catch (java.sql.SQLException e) {
con = getConnection();
}
} else {
con = newConnection();
}
// 连接池限额检测
if (this.maxConn == 0 || this.maxConn <= this.inUsed) {
System.out.println("连接池 " + this.poolname + ",最大限额" + this.maxConn
+ "现有" + inUsed + "个连接正在使用,您的请求无法响应!");
con = null;
}
// 连接池使用情况设置
if (con != null) {
this.inUsed++;
System.out.println("已获得连接池 " + this.poolname + "的数据库连接资源,连接池最大限额"
+ this.maxConn + "现有" + inUsed + "个连接正在使用!");
}
return con;
}
/*
* 新增数据库连接资源 注释:创建数据库连接
*/
private Connection newConnection() {
// TODO Auto-generated method stub
Connection con = null;
try {
Class.forName(dbdriver);
con = DriverManager.getConnection(dburl, dbuser, dbpassword);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("数据库驱动程序无法正常运行,请检查驱动设置信息!");
} catch (java.sql.SQLException e1) {
e1.printStackTrace();
System.out.println("无法正常创建数据库连接,请检测数据库网络通讯情况!");
}
return con;
}
/*
* 回收数据库连接 注释:数据库操作完成,将连接资源归还给连接池,给其他需要使用的业务使用
*/
public synchronized void freeConnection(Connection con) {
this.freeConnections.add(con);
this.inUsed--;
}
/*
* 释放连接池资源 注释:取消资源池的数据库连接资源
*/
public synchronized void releasePool() {
if (this.freeConnections.size() > 0) {
Connection con = null;
Iterator allConns = this.freeConnections.iterator();
while (allConns.hasNext()) {
con = (Connection) allConns.next();
try {
if (con != null)
con.close();
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
this.freeConnections.clear();
}
}
/**
* @return the minConn
*/
public int getMinConn() {
return minConn;
}
/**
* @param minConn
* the minConn to set
*/
public void setMinConn(int minConn) {
this.minConn = minConn;
}
/**
* @return the maxConn
*/
public int getMaxConn() {
return maxConn;
}
/**
* @param maxConn
* the maxConn to set
*/
public void setMaxConn(int maxConn) {
this.maxConn = maxConn;
}
/**
* @return the poolname
*/
public String getPoolname() {
return poolname;
}
/**
* @param poolname
* the poolname to set
*/
public void setPoolname(String poolname) {
this.poolname = poolname;
}
/**
* @return the dbpassword
*/
public String getDbpassword() {
return dbpassword;
}
/**
* @param dbpassword
* the dbpassword to set
*/
public void setDbpassword(String dbpassword) {
this.dbpassword = dbpassword;
}
/**
* @return the dbuser
*/
public String getDbuser() {
return dbuser;
}
/**
* @param dbuser
* the dbuser to set
*/
public void setDbuser(String dbuser) {
this.dbuser = dbuser;
}
/**
* @return the dbdriver
*/
public String getDbdriver() {
return dbdriver;
}
/**
* @param dbdriver
* the dbdriver to set
*/
public void setDbdriver(String dbdriver) {
this.dbdriver = dbdriver;
}
/**
* @return the dburl
*/
public String getDburl() {
return dburl;
}
/**
* @param dburl
* the dburl to set
*/
public void setDburl(String dburl) {
this.dburl = dburl;
}
}
如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!
赞0
添加新评论0 条评论