📄 connpool.java
字号:
package DBConnection ;
import java.lang.* ;
import java.sql.* ;
import java.util.* ;
import java.io.InputStream;
//本连接池技术不保持连接计数。无多个线程共用一个connection的情况
public class ConnPool {
private static final int defaultMaxConnections=3 ;
private Vector freeConnections ; //空闲连接池
private Hashtable boundConnections ; //已分配连接池
private String driverName ;
private String jdbcURL ;
private String username ;
private String password ;
private int maxConnections ;
private static ConnPool instance = null;
public static ConnPool getInstance(){
if (instance==null)
instance = new ConnPool();
return instance;
}
// ------------------------------------ Constructot -------------------------------------
public ConnPool( int numConnections ) {
maxConnections=numConnections ;
boundConnections=null ;
freeConnections=null ;
driverName="" ;
jdbcURL="" ;
username="" ;
password="" ;
}
// --------------------------------------------------------------------------------------
public ConnPool() {
this( defaultMaxConnections ) ;
setPropsFileName("db.properties");
try{
setConnectionSwitch("ON");
}catch (Exception e){
e.printStackTrace();
}
}
public void closeDB() throws SQLException {
if( boundConnections!=null ) {
for( Enumeration e=boundConnections.elements() ; e.hasMoreElements() ; ) {
Connection conn=(Connection)e.nextElement() ;
conn.close() ;
}
boundConnections.clear() ;
boundConnections=null ;
}
if( freeConnections!=null ) {
for( Enumeration e=freeConnections.elements() ; e.hasMoreElements() ; ) {
Connection conn=(Connection)e.nextElement() ;
conn.close() ;
}
freeConnections.removeAllElements() ;
freeConnections=null ;
}
}
public synchronized Connection getConnection()
throws SQLException {
if( freeConnections==null )
throw new SQLException( "ERROR : The conection pool still has not been established yet." ) ;
if( boundConnections.get( Thread.currentThread() )!=null ){//同一线程不允许多次connect数据库
//Modifide by chenyuan 04-10
System.err.println( "ERROR : Cannot get connections over once for this current running thread." ) ;
returnConnection();
}
try {
if( freeConnections.size()==0 ) //无空闲connect可分配了
wait() ;
}
catch( InterruptedException ex ) {
throw new SQLException( ex.toString() ) ;
}
//System.err.println("The free thread number is "+freeConnections.size());
Connection conn=(Connection)freeConnections.firstElement() ;
freeConnections.removeElement( conn ) ;
boundConnections.put( Thread.currentThread(), conn ) ;
return conn ;
}
public void openDB( String drvName, String url,
String uname, String passwd )
throws SQLException {
try {
boundConnections=new Hashtable( maxConnections ) ;
freeConnections=new Vector( maxConnections ) ;
Class.forName( drvName ) ;
for( int i=0 ; i<maxConnections ; i++ )//预先建立好max个连接放在空闲池中等待分配
freeConnections.addElement( DriverManager.getConnection( url ,uname ,passwd ) ) ;
}
catch( Exception ex ) {
boundConnections=null ;
freeConnections=null ;
throw new SQLException( ex.toString() ) ;
}
}
public synchronized void returnConnection()
throws SQLException {
Connection conn=(Connection)boundConnections.remove( Thread.currentThread() ) ;
if( conn==null )
throw new SQLException( "ERROR : The connection which this current running thread got is not found." ) ;
freeConnections.addElement( conn ) ;
notify() ;
}
//public void setConnectionSwitch( String on_off ) throws ServletException {
public void setConnectionSwitch( String on_off ) throws Exception {
try {
if( on_off.equalsIgnoreCase( "ON" ) )
openDB( driverName, jdbcURL, username, password ) ;
else if( on_off.equalsIgnoreCase( "OFF" ) )
closeDB() ;
}
catch( SQLException ex ) {
//throw new ServletException( ex.toString() ) ;
throw new Exception( ex.toString() ) ;
}
}
public void setMaxConnections( int numConnections ) {
maxConnections=numConnections ;
}
public void setDriverName( String drvName ) {
driverName=drvName ;
}
public void setJdbcURL( String url ) {
jdbcURL=url ;
}
public void setUserName( String uname ) {
username=uname ;
}
public void setPassword( String passwd ) {
password=passwd ;
}
public void setPropsFileName(String FileName){
InputStream is = getClass().getResourceAsStream(FileName);
Properties dbProps = new Properties();
try {
dbProps.load(is);
}
catch (Exception e) {
System.err.println("ERROR : 不能读取属性文件. " +"请确保db.properties在CLASSPATH指定的路径中");
return;
}
String drvName=dbProps.getProperty("Drivers");
String url=dbProps.getProperty("Url");
String uName=dbProps.getProperty("UserName");
String uPsw=dbProps.getProperty("PassWord");
String maxconn = dbProps.getProperty("MaxConnectionNum", "0");
int max;
try {
max = Integer.valueOf(maxconn).intValue();
}
catch (NumberFormatException e) {
max = 0;
}
setDriverName(drvName);
setJdbcURL(url);
setUserName(uName);
setPassword(uPsw);
setMaxConnections(max);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -