中秋快乐!内存溢出.无法解决.放段代码大家共享.

lifejoy 2003-09-09 10:46:44
这是偶模拟.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);
}

...全文
13 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
lizero 2003-09-12
DBConnectionPool 中的getConnection() 好像有点问题:

=============================
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;
}

}

}
====================================
这一段中当取到一个后应该立刻break吧。

另提一点建议:
pool最好不要用static方式,使用对象实例的方式(动态方法)实现更好些,适用的场合更多。即使目前不需要也可以使用单子的方式做,以后开放了对调用者代码的影响小些。另,如果想做得更宽泛,先定义一个ConnectionPool的接口,允许提供不同机制的实现,由一个工厂(或抽象工厂)实现ConnectionPool实例产生逻辑。
synchronized public static Connection getConnection() throws SQLException {
PoolConnection pConnection = null;
// int aCount=pConnectionVector.size();

if (pConnection == null) {
pConnection = getNewConnection();
pConnectionVector.add(pConnection);
}
return pConnection;

}
回复
zcjl 2003-09-09
Thanks
等我有空了看看
顺祝楼主中秋快乐
楼下的么……就请我吃月饼吧
:)
回复
lifejoy 2003-09-09
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws
SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method createStatement() not yet implemented.");
return aCon.createStatement(resultSetType, resultSetConcurrency);
}

public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws
SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented.");
return aCon.prepareStatement(sql, resultSetType, resultSetConcurrency);
}

public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency) throws
SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareCall() not yet implemented.");
return aCon.prepareCall(sql, resultSetType, resultSetConcurrency);
}

public Map getTypeMap() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method getTypeMap() not yet implemented.");
return aCon.getTypeMap();
}

public void setTypeMap(Map map) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method setTypeMap() not yet implemented.");
aCon.setTypeMap(map);
}

public void setHoldability(int holdability) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method setHoldability() not yet implemented.");
aCon.setHoldability(holdability);
}

public int getHoldability() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method getHoldability() not yet implemented.");
return aCon.getHoldability();
}

public Savepoint setSavepoint() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method setSavepoint() not yet implemented.");
return setSavepoint();
}

public Savepoint setSavepoint(String name) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method setSavepoint() not yet implemented.");
return setSavepoint(name);
}

public void rollback(Savepoint savepoint) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method rollback() not yet implemented.");
aCon.rollback(savepoint);
}

public void releaseSavepoint(Savepoint savepoint) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method releaseSavepoint() not yet implemented.");
aCon.releaseSavepoint(savepoint);
}

public Statement createStatement(int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws
SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method createStatement() not yet implemented.");
return aCon.createStatement(resultSetType, resultSetConcurrency,
resultSetHoldability);
}

public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws
SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented.");
return aCon.prepareStatement(sql, resultSetType, resultSetConcurrency,
resultSetHoldability);
}

public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws
SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareCall() not yet implemented.");
return aCon.prepareCall(sql, resultSetType, resultSetConcurrency,
resultSetHoldability);
}

public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws
SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented.");
return aCon.prepareStatement(sql, autoGeneratedKeys);
}

public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws
SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented.");
return aCon.prepareStatement(sql, columnIndexes);
}

public PreparedStatement prepareStatement(String sql, String[] columnNames) throws
SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented.");
return aCon.prepareStatement(sql, columnNames);
}

public void closeConnection() throws SQLException {
if (onClose != null) {
onClose.Action(this);
}
aCon.close();

}

public boolean isUsed() {
return inUse;

}

public void use() {
inUse = true;
}

public void setOnClose(OnConnectionClose Action) {
onClose = Action;

}

}
==========================================================================
主要做法就是生成一个连接warp,每次取回的连接不是当前Drivers的实例,而是一个包装类PoolConnection的实例,PoolConnection的包装是为了调度.但是对于PoolConnection实例的调用将被传递到Driver的Connection实例:)
回复
lifejoy 2003-09-09
接上面代码:
public String nativeSQL(String sql) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method nativeSQL() not yet implemented.");
return aCon.nativeSQL(sql);
}

public void setAutoCommit(boolean autoCommit) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method setAutoCommit() not yet implemented.");
aCon.setAutoCommit(autoCommit);
}

public boolean getAutoCommit() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method getAutoCommit() not yet implemented.");
return aCon.getAutoCommit();
}

public void commit() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method commit() not yet implemented.");
aCon.commit();
}

public void rollback() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method rollback() not yet implemented.");
aCon.rollback();
}

public void close() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method close() not yet implemented.");
if(DBConnectionPool.getCout()<=DBConnectionPool.getMinCount())
{
inUse = false;
}else
{
closeConnection();
}


}

public boolean isClosed() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method isClosed() not yet implemented.");
return closed;
}

public DatabaseMetaData getMetaData() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method getMetaData() not yet implemented.");
return aCon.getMetaData();
}

public void setReadOnly(boolean readOnly) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method setReadOnly() not yet implemented.");
aCon.setReadOnly(readOnly);
}

public boolean isReadOnly() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method isReadOnly() not yet implemented.");
return isReadOnly();
}

public void setCatalog(String catalog) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method setCatalog() not yet implemented.");
aCon.setCatalog(catalog);
}

public String getCatalog() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method getCatalog() not yet implemented.");
return aCon.getCatalog();
}

public void setTransactionIsolation(int level) throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method setTransactionIsolation() not yet implemented.");
aCon.setTransactionIsolation(level);
}

public int getTransactionIsolation() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method getTransactionIsolation() not yet implemented.");
return aCon.getTransactionIsolation();
}

public SQLWarning getWarnings() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
// throw new java.lang.UnsupportedOperationException("Method getWarnings() not yet implemented.");
return aCon.getWarnings();
}

public void clearWarnings() throws SQLException {
/**@todo Implement this java.sql.Connection method*/
//throw new java.lang.UnsupportedOperationException("Method clearWarnings() not yet implemented.");
aCon.clearWarnings();
}

回复
相关推荐
发帖
Java SE
创建于2007-09-28

6.1w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2003-09-09 10:46
社区公告
暂无公告