小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

數(shù)據(jù)庫(kù)的連接池(經(jīng)典的那種)

 昵稱10987 2006-09-08
數(shù)據(jù)庫(kù)的連接池(經(jīng)典的那種)

package com.jewes;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;

//建立DBConnectionManager
public class DBConnectionManager
{static private DBConnectionManager instance;
static private int clients;

private Vector drivers=new Vector();
private PrintWriter log;
private Hashtable pools=new Hashtable();

//返回唯一的實(shí)列
static synchronized public DBConnectionManager getInstance()
{if(instance==null)
{instance=new DBConnectionManager();
} clients++;
return instance;
}

//構(gòu)造函數(shù)!
private DBConnectionManager()
{init();
} //結(jié)束構(gòu)造函數(shù)
//釋放一個(gè)連接
public void freeConnection(String name,Connection con)
{DBConnectionPool pool=(DBConnectionPool)pools.get(name);
if(pool!=null)
{pool.freeConnection(con);
} }
//結(jié)束釋放一個(gè)連接

//取得一個(gè)連接
public Connection getConnection(String name)
{DBConnectionPool pool=(DBConnectionPool)pools.get(name);
if(pool!=null)
{return pool.getConnection();
} return null;
}

public Connection getConnection(String name,long time)
{DBConnectionPool pool=(DBConnectionPool)pools.get(name);
if(pool!=null)
{return pool.getConnection(time);
} return null;
} //結(jié)束getconnection
//關(guān)閉所有連接
public synchronized void release()
{ {if(--clients!=0)
return;
} Enumeration allPools=pools.elements();
while(allPools.hasMoreElements())
{DBConnectionPool pool=(DBConnectionPool)allPools.nextElement();
pool.release();
} Enumeration allDrivers=drivers.elements();
while(allDrivers.hasMoreElements())
{Driver driver=(Driver)allDrivers.nextElement();
try
{DriverManager.deregisterDriver(driver);
log("撤消JDBC驅(qū)動(dòng)程序"+driver.getClass().getName());
} catch(SQLException e)
{log(e,"無(wú)法撤消JDBC驅(qū)動(dòng)程序的注冊(cè)"+driver.getClass().getName());
} }
} private void createPools(Properties props)
{Enumeration propNames=props.propertyNames();
while(propNames.hasMoreElements())
{String name=(String) propNames.nextElement();
if(name.endsWith(".url"))
{String poolName=name.substring(0,name.lastIndexOf("."));
String url=props.getProperty(poolName+".url");
if(url==null)
{log("沒(méi)有連接池"+poolName+"指定的URL");
continue;
} String user=props.getProperty(poolName+".user");
String password=props.getProperty(poolName+".password");
String maxconn= props.getProperty(poolName+".maxconn","0");
int max;
try
{max=Integer.valueOf(maxconn).intvalue();
} catch(NumberFormatException e)
{log("錯(cuò)誤的最大連接數(shù):"+maxconn+".連接池"+poolName);
max=0;
} DBConnectionPool pool=new DBConnectionPool(poolName,url,user,password,max);
pools.put(poolName,pool);
log("成功創(chuàng)建連接池"+poolName);
} }
}

private void init()
{InputStream is=getClass().getResourceAsStream("/db.properties");
Properties dbProps=new Properties();
try
{dbProps.load(is);
} catch(Exception e)
{System.err.println("不能讀取屬性文件。請(qǐng)確保db.properties在你的CLASSPATH中");
return;
} String logFile=dbProps.getProperty("logfile","DBConnectionManager.log");
try
{log=new PrintWriter(new FileWriter(logFile,true),true);
} catch(IOException e)
{System.err.println("無(wú)法打開(kāi)日志文件:"+logFile);
log=new PrintWriter(System.err);
} loadDriver(dbProps);
createPools(dbProps);
}

private void loadDriver(Properties props)
{String driverClasses=props.getProperty("drivers");
StringTokenizer st=new StringTokenizer(driverClasses);
while(st.hasMoreElements())
{String driverClassName=st.nextToken().trim();
try
{Driver driver=(Driver)Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver);
log("成功注冊(cè)驅(qū)動(dòng)程序"+driverClassName);
} catch(Exception e)
{log("無(wú)法注冊(cè)驅(qū)動(dòng)程序:"+driverClassName+",錯(cuò)誤"+e);
} }
}

private void log(String msg)
{log.println(new Date()+":"+msg);
} private void log(Throwable e,String msg)
{log.println(new Date()+":"+msg);
e.printStackTrace(log);
} class DBConnectionPool
{private int checkOut;
private Vector freeConnections=new Vector();
private int maxconn;
private String name;
private String password;
private String URL;
private String user;

public DBConnectionPool(String name,String URL,String user,String password,int maxconn)
{this.name=name;
this.URL=URL;
this.password=password;
this.user=user;
this.maxconn=maxconn;
} public synchronized void freeConnection(Connection con)
{freeConnections.addElement(con);
checkOut--;
notifyAll();
} public synchronized Connection getConnection()
{Connection con=null;
if(freeConnections.size()>0)
{con=(Connection)freeConnections.firstElement();
freeConnections.removeElementAt(0);
try
{if(con.isClosed())
{log("從連接池"+name+"刪除一個(gè)連接");
con=getConnection();
} }
catch(SQLException e)
{log("從連接池"+name+"刪除一個(gè)連接");
con=getConnection();
} }
else if(maxconn==0||checkOut<maxconn)
{con=newConnection();
} if(con!=null)
{checkOut++;
} return con;
}

public synchronized Connection getConnection(long timeout)
{long startTime=new Date().getTime();
Connection con;
while((con=getConnection())==null)
{ try
{wait(timeout);
} catch(InterruptedException e)
{}
if((new Date().getTime()-startTime)>=timeout)
{return null;
} }
return con;
} public void release()
{Enumeration allConnections=freeConnections.elements();
while(allConnections.hasMoreElements())
{Connection con=(Connection)allConnections.nextElement();
try
{con.close();
log("關(guān)閉連接池"+name+"中的連接");
} catch(SQLException e)
{log(e,"無(wú)法關(guān)閉連接池"+name+"中的連接");
} }
freeConnections.removeAllElements();
} private Connection newConnection()
{Connection con=null;
try
{con=DriverManager.getConnection(URL,user,password);
log("連接池"+name+"創(chuàng)建一個(gè)新的連接");
} catch(SQLException e)
{log(e,"無(wú)法創(chuàng)建下列URL的連接"+URL);
return null;
} return con;
} }
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多