中秋快乐!内存溢出.无法解决.放段代码大家共享.
这是偶模拟.Net的Connection Pool写的一个连接池还不是太完善,没有动态回收机制.但是也是可以大幅度提高性能DI.希望大家笑纳.方法简单可以加入现有的系统中只需改变连接方式即可;目前没有提供多数据连接.只提高了连接一个数据库的方法.抱歉.以后有空再加吧.或者那位老兄有心也可以加上.
==========================================================================
package DBTools;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.sql.*;
import java.util.*;
interface OnConnectionClose {
public void Action(PoolConnection sender);
}
public class DBConnectionPool {
private static Vector pConnectionVector = new Vector();
// private static int Count=0;
private static int minCount = 1;
private static String URL = "";
private static String User = "";
private static String Password = "";
private static String DriverName="";
synchronized public static void setMinCount(int Value) {
minCount = Value;
}
synchronized public static int getMinCount() {
return minCount;
}
synchronized public static int getCout() {
return pConnectionVector.size();
}
synchronized public static Connection getConnection() throws SQLException {
PoolConnection pConnection = null;
// int aCount=pConnectionVector.size();
for (int i = 0; i < pConnectionVector.size(); i++) {
Object oCon = pConnectionVector.elementAt(i);
if (oCon instanceof PoolConnection) {
PoolConnection aCon = (PoolConnection) oCon;
if (!aCon.isUsed()) {
pConnection = aCon;
}
}
}
if (pConnection == null) {
pConnection = getNewConnection();
pConnectionVector.add(pConnection);
}
return pConnection;
}
private static PoolConnection getNewConnection() throws SQLException {
try
{
Class.forName( DriverName);
}catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
PoolConnection con = new PoolConnection(URL, User, Password);
con.setOnClose(new theOnClose(pConnectionVector));
return con;
}
synchronized public static void SetJDBC(String url, String user, String password) {
URL = url;
User = user;
Password = password;
}
synchronized public static void setURL(String url) {
URL = url;
}
synchronized public static String getUrl() {
return URL;
}
synchronized public static void setUser(String user)
{
User=user;
}
synchronized public static String getUser()
{
return User;
}
synchronized public static void setPassword(String password)
{
Password=password;
}
synchronized public static String getPassword()
{
return Password;
}
synchronized public static void setDriverName(String dName)
{
DriverName=dName;
}
synchronized public static String getDriverName()
{
return DriverName;
}
}
class theOnClose
implements OnConnectionClose {
private Vector v;
public theOnClose(Vector vt) {
v = vt;
}
public void Action(PoolConnection sender) {
v.remove(sender);
}
}
class PoolConnection
implements Connection {
private Connection aCon = null;
private boolean closed = false;
private boolean inUse = false;
private String DriverName;
private OnConnectionClose onClose = null;
protected PoolConnection() {
}
public PoolConnection(String Url, String User, String Password) throws
SQLException {
aCon = DriverManager.getConnection(Url, User, Password);
closed = false;
inUse=true;
}
public PoolConnection(String Url) throws Exception {
aCon = DriverManager.getConnection(Url);
closed = false;
inUse=true;
}
public Statement createStatement() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method createStatement() not yet implemented.");
return aCon.createStatement();
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented.");
return aCon.prepareStatement(sql);
}
public CallableStatement prepareCall(String sql) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareCall() not yet implemented.");
return aCon.prepareCall(sql);
}