📄 dbconnectionpool.java~1~
字号:
/**
* This inner class represents a connection pool. It creates new
* connections on demand, up to a max number if specified.
* It also makes sure a connection is still open before it is
* returned to a client.
*/
package com.lazybug.sql;
import java.io.*;
import java.sql.*;
import java.util.*;
import com.lazybug.util.*;
public class DBConnectionPool extends Listener
{
static final long INTERVAL = 30000; //监控线程的监控间隔
public int checkedOut = 0;
private LinkedList m_listConn = new LinkedList();
private String driver;
private String password;
private String URL;
private String user;
public DBConnectionPool()
{
this.start();
}
/**
* Creates new connection pool.
*
* @param name The pool name
* @param URL The JDBC URL for the database
* @param user The database user, or null
* @param password The database user password, or null
* @param maxConn The maximal number of connections, or 0
* for no limit
*/
public DBConnectionPool(String driver, String url, String user, String password)
throws Exception
{
this.driver = driver;
this.URL = url;
this.user = user;
this.password = password;
Class.forName(this.driver).newInstance();
DBConnection c = this.getConnection();
if( c == null ) throw new Exception();
this.freeConnection(c);
this.start();
}
public synchronized void close()
{
this.stop();
this.notifyAll();
}
/**
* Checks in a connection to the pool. Notify other Threads that
* may be waiting for a connection.
*
* @param con The connection to check in
*/
public synchronized void freeConnection(DBConnection con)
{
// Put the connection at the end of the Vector
// Log.logMessage(this, "释放一个数据库"+con.dbconn_id+"!");
m_listConn.addLast(con);
checkedOut -= 1;
notifyAll();
}
/**
* Checks out a connection from the pool. If no free connection
* is available, a new connection is created unless the max
* number of connections has been reached. If a free connection
* has been closed by the database, it's removed from the pool
* and this method is called again recursively.
*/
public synchronized DBConnection getConnection()
{
DBConnection dbconn = null;
if ( m_listConn.size() > 0)
{
dbconn = (DBConnection) m_listConn.removeFirst();
//Log.logMessage(this, "从连接池获取了一个数据库"+dbconn.dbconn_id+"!");
}
else
{
int capacity = ConfigUtil.getInteger("db_poolcapacity");
if (capacity == 0 || checkedOut < capacity)
{
dbconn = newConnection();
//Log.logMessage(this, "创建了一个新的数据库"+dbconn.dbconn_id+"!");
}
else
{
Log.logWarning(this, "数据库连接池已经到了最大限度!" + this.toString());
}
}
if (dbconn != null) checkedOut += 1;
return dbconn;
}
/**
* Checks out a connection from the pool. If no free connection
* is available, a new connection is created unless the max
* number of connections has been reached. If a free connection
* has been closed by the database, it's removed from the pool
* and this method is called again recursively.
* <P>
* If no connection is available and the max number has been
* reached, this method waits the specified time for one to be
* checked in.
*
* @param timeout The timeout value in milliseconds
*/
public DBConnection getConnection(long timeout)
{
long startTime = System.currentTimeMillis();
DBConnection dbconn = null;
while ( (dbconn = getConnection()) == null)
{
synchronized( this )
{
try
{
wait(10);
//System.out.println("发生等待获取数据库连接的事情!!!!!!!!!!!!!");
}
catch (InterruptedException e)
{}
}
if ( (System.currentTimeMillis() - startTime) >= timeout )
{
//Log.logWarning(this, "获取数据库连接超时!");
break;
}
}
return dbconn;
}
/**
* Closes all available connections.
*/
private synchronized void release()
{
DBConnection dbconn = null;
try
{
while ( (dbconn = (DBConnection)this.m_listConn.removeLast()) != null)
{
dbconn.close();
checkedOut -= 1;
}
}
catch(NoSuchElementException e)
{
}
catch(SQLException e)
{
}
}
/**
* Creates a new connection, using a userid and password
* if specified.
*/
private DBConnection newConnection()
{
DBConnection con = null;
try
{
if (user == null)
con = new DBConnection(URL);
else
con = new DBConnection(URL, user, password);
}
catch (SQLException e)
{
Log.logError(this, "Fail to create DBS connection!");
return null;
}
return con;
}
/**
* 该线程用来检查连接池中超时的对象
* @see java.lang.Runnable#run()
*/
public void run()
{
this.remark = "[数据库连接池]";
Log.logMessage(this, "DBS connection pool listener is start.");
while( this.isRunning )
{
for( int k = 0; k < m_listConn.size(); k++ )
{
DBConnection dbconn = getConnection();
try
{
if ( dbconn.validPeriod() < System.currentTimeMillis() )
{
Log.logMessage(this, "["+dbconn.dbconn_id+"]已经过期,资源被释放!");
dbconn.close();
checkedOut -= 1;
}
else
freeConnection(dbconn);
}
catch(Exception e)
{
Log.getInstance().write("释放数据库连接异常"+e);
}
}
synchronized( this ){
try{
this.isBusy = false;
wait(INTERVAL);
this.isBusy = true;
}catch(InterruptedException e){}
}
}
this.isBusy = false;
release();
Log.logMessage(this, "DBS connection pool listener end.Connection resources is release!");
}
public String toString()
{
StringBuffer strBf = new StringBuffer();
strBf.append("忙连接["+checkedOut);
strBf.append("]闲连接["+m_listConn.size());
strBf.append("]");
return strBf.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -