java ee-数据源连接池DriverPool

DriverPool类,直接由数据库驱动程序构建,便于开发和调试单机高效的非事务程序

Public class DriverPool implements IConnPool{

private String driverName;

private String conURL;

private username;

private password;

private int min,max;

Vector conPool;

private Driver drv;

private class PooledConnection{

private Connection con;

privateboolean available;

private PooledConnection(Connection con,boolean available){

this.con=con;

this.available=available;

}

Connection getConnection(){

return con;

}

boolean isAvailable(){

return available;

}

void setAvailable(boolean available){

conAvailable=available;

}

}

//构造器

public DriverPool(int max,int min,String driverName,String conURL,String username,String password){

this.driverName=driverName;

this.conURL=conURL;

this.username=username;

this.password=password;

this.max=max;

this.min=min;

conPool=new Vector();

addConnectionsToPool(this.min);

//连接池后台监控启动

new PoolMonitor().startNow();

}

private synchronized void addConnectionsToPool(int numPooledCon){

try{

if(drv==null){

drv=(Driver) Class.forName(driverName).netIntance();

DriverManger.registerDriver(drv);

}

for(int i=0;i<numPooledCon;i++){

Connection con=DriverManger.getConnection(conURL,username,password);

PooledConnection pc=newPooledConnection(con,true);

conPool.add(pc);

}

}

catch(Exception e){

e.printStackTrace();

}

}

///监控连接池中的情况,实时回收无效的连接和根据要求往连接池中添加有效地连接,在监控线程中调用

private void moniPool{

removeAnyClosedConnections(); //删除所有无效的连接

if(this.conPool.size()<this.min){

int x=(this.max-this.min)/2;

int y=this.min-this.conPool.size();

if(x>0){

if(x+j>this.max){

this.addConnectionsToPool(x);

}

else{

this.addConnectionsToPool(x+j);

}

}

else(

this.addConnectionToPool(j);

}

}

}

}

private synchronized void removeAnyClosedConnection(){

try{

boolean done=false;

while(!done){

done=true;

for(int i=0;i<conPool.size();i+=){

PooledConnection pc=(PooledConnection) conPool.get(i);

if(pc.getConnection().isClosed()){

conPool.remove();

done=false;

break;

}

}

}

}

catch(SQLException e){

e.printStackTrace();

}

}

//从连接池中取出一个有效的连接

public synchronized Connection getConnection(){

//从池中找出一个有效的连接,返回

for(i=0;i<conPool.size();i++)

{

PooledConnection pc=(PooledConnection) conPool.get(i);

if(pc.isAvailable(){

pc.setAvailbale(false);

return pc.getConnection();

}

}

//如果池中没有就加一个,并返回,保证每个请求都有有效的连接返回

addConnectionsToPool(1);

PooledConnection pc=(PooledConnection) conPool.size()-1);

if(pc.isAvailable()){

pc.setAvailable(false);

}

return pc.getConnection();

}

//关闭连接池中的所有连接

public void closeAllConnections(){

for(int i=0;i<conPool.size();i++){

PooledConnection pc=(PooledConnecion) conPool.get(i);

closeConnection(pc.getConnection());

}

conPool.clear();

}

private void closeConnection(Connection con){

try{

if(con!=null){

con.close();

}

}

catch(SQLException e){

e.printStackTrace();

}

}

//后台监控线程,每秒监控一次

private class PoolMonitor

extends com.beetle.framework.appsrv.AppRunnable{

privat int time;

public PoolMonitor(){

super();

time=1000;

}

protected void end(){

closeAllConnections();

}

public void run(){

while(!this.getStopFlag()){

moniPool();

sleep(time);

}

}

}

}

你可能感兴趣的:(java,J#)