?? connectionpool.java
字號:
/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author chenlong
* @version 1.0
*/
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Vector;
/**
*\uF06C \u4ECE\u6570\u636E\u5E93\u6C60\u4E2D\u83B7\u5F97\u4E00\u4E2A\u6253\u5F00\u7684\u8FDE\u63A5
* \uF06C \u8FD4\u56DE\u4E00\u4E2A\u8FDE\u63A5\u5230\u6570\u636E\u5E93\u6C60
* \uF06C \u5728\u7CFB\u7EDF\u8FD0\u884C\u505C\u6B62\u65F6\uFF0C\u91CA\u653E\u6240\u6709\u7684\u8D44\u6E90\u5E76\u5173\u95ED\u6240\u6709\u7684\u8FDE\u63A5
*/
public class ConnectionPool
{
private String name;
private String URL;
private String user;
private String password;
/**
* \u6700\u5927\u8FDE\u7ED3\u6570
*/
private int maxConns;
/**
* \u7B49\u5F85\u65F6\u95F4
*/
private int timeOut;
private LogWriter logWriter;
/**
* \u88AB\u53D6\u51FA\u7684\u8FDE\u63A5\u7684\u6570\u76EE
*/
private int checkedOut;
/**
* \u53EF\u4EE5\u4F7F\u7528\u7684\u8FDE\u63A5\u653E\u5728\u6B64\u5411\u91CF\u4E2D
*/
private Vector freeConnections = new Vector();
public ConnectionPool(String name, String URL, String user,
String password, int maxConns, int initConns, int timeOut,
PrintWriter pw, int logLevel)
{
this.name = name;
this.URL = URL;
this.user = user;
this.password = password;
this.maxConns = maxConns;
this.timeOut = timeOut > 0 ? timeOut : 5;
logWriter = new LogWriter(name, logLevel, pw);
initPool(initConns);
logWriter.log("New pool created", LogWriter.INFO);
String lf = System.getProperty("line.separator");
logWriter.log(lf +
" url=" + URL + lf +
" user=" + user + lf +
" password=" + password + lf +
" initconns=" + initConns + lf +
" maxconns=" + maxConns + lf +
" logintimeout=" + this.timeOut, LogWriter.DEBUG);
logWriter.log(getStats(), LogWriter.DEBUG);
}
/**
* \u521D\u59CB\u5316\u8FDE\u63A5\u6C60
*/
private void initPool(int initConns)
{
for (int i = 0; i < initConns; i++)
{
try
{
Connection pc = newConnection();
freeConnections.addElement(pc);
}
catch (SQLException e)
{ e.printStackTrace();}
}
}
/**
* Get a pooled Connection from the cache or a new one.
* Wait if all are checked out and the max limit has
* been reached.
*/
public Connection getConnection() throws SQLException
{
logWriter.log("Request for connection received", LogWriter.DEBUG);
try
{
return getConnection(timeOut * 1000);
}
catch (SQLException e)
{
logWriter.log(e, "Exception getting connection",
LogWriter.ERROR);
throw e;
}
}
/**
* Get a pooled Connection from the cache or a new one.
* Wait if all are checked out and the max limit has
* been reached.
* @param timeout \u6700\u5927\u7B49\u5F85\u65F6\u95F4
*/
private synchronized Connection getConnection(long timeout)
throws SQLException
{
long startTime = System.currentTimeMillis();
long remaining = timeout;
Connection conn = null;
while ((conn = getPooledConnection()) == null)
{
try
{
logWriter.log("Waiting for connection. Timeout=" + remaining,
LogWriter.DEBUG);
wait(remaining);
}
catch (InterruptedException e)
{e.printStackTrace(); }
remaining = timeout - (System.currentTimeMillis() - startTime);
if (remaining <= 0)
{
// Timeout has expired
logWriter.log("Time-out while waiting for connection",
LogWriter.DEBUG);
throw new SQLException("getConnection() timed-out");
}
}
// Check if the Connection is still OK
if (!isConnectionOK(conn))
{
// It was bad. Try again with the remaining timeout
logWriter.log("Removed selected bad connection from pool",
LogWriter.ERROR);
return getConnection(remaining);
}
checkedOut++;
logWriter.log("Delivered connection from pool", LogWriter.INFO);
logWriter.log(getStats(), LogWriter.DEBUG);
return conn;
}
private boolean isConnectionOK(Connection conn)
{
Statement testStmt = null;
try
{
if (!conn.isClosed())
{
// Try to createStatement to see if it's really alive
testStmt = conn.createStatement();
testStmt.close();
}
else
{
return false;
}
}
catch (SQLException e)
{
if (testStmt != null)
{
try
{
testStmt.close();
}
catch (SQLException se)
{ e.printStackTrace(); }
}
logWriter.log(e, "Pooled Connection was not okay",
LogWriter.ERROR);
return false;
}
return true;
}
/**
* Get a pooled Connection from the cache
*/
private Connection getPooledConnection() throws SQLException
{
Connection conn = null;
if (freeConnections.size() > 0)
{
// Pick the first Connection in the Vector
// to get round-robin usage
conn = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
}
else if (maxConns == 0 || checkedOut < maxConns)
{
conn = newConnection();
}
return conn;
}
/**
* Create a new pooled Connection
*/
private Connection newConnection() throws SQLException
{
Connection conn = null;
if (user == null)
{
conn = DriverManager.getConnection(URL);
}
else
{
conn = DriverManager.getConnection(URL, user, password);
}
logWriter.log("Opened a new connection", LogWriter.INFO);
return conn;
}
/**
* free a connection to pool
*/
public synchronized void freeConnection(Connection conn)
{
// Put the connection at the end of the Vector
if (this.isConnectionOK(conn))
{
if(!freeConnections.contains(conn))
{
freeConnections.addElement(conn);
checkedOut--;
notifyAll();
logWriter.log("Returned connection to pool", LogWriter.INFO);
logWriter.log(getStats(), LogWriter.DEBUG);
}
}else
{
System.out.println("You Check in a Wrong Connection!!!");
}
}
/**
* release all connection from pool
*/
public synchronized void release()
{
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements())
{
Connection con = (Connection) allConnections.nextElement();
try
{
con.close();
logWriter.log("Closed connection", LogWriter.INFO);
}
catch (SQLException e)
{
e.printStackTrace();
logWriter.log(e, "Couldn't close connection", LogWriter.ERROR);
}
}
freeConnections.removeAllElements();
}
/**
* get the statement of the connection pool
*/
private String getStats() {
return "Total connections: " +
(freeConnections.size() + checkedOut) +
" Available: " + freeConnections.size() +
" Checked-out: " + checkedOut;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -