分享
 
 
 

Java模拟.NET的连接池

王朝java/jsp·作者佚名  2007-06-08
窄屏简体版  字體: |||超大  

作者 : manboo

标题 : Java模拟.NET的连接池

关键字: Java;.NET;连接池

分类 : 开发经验

密级 : 参赛

(评分:★★★★ , 回复: 2, 阅读: 512)

在之前:

.NET的ADO.NET的本身包含连接池功能,而java是在第三方开发包中提高的连接池功能因此,需要去下载第三方的连接池包,但是java的连接池一般都是在EJB或者B/S系统中使用的(虽然也有C/S下的连接池如Borland 在Jbuilder中提供的),在一个服务性系统中使用起来不是很方便.再说使用第三方的开发包也不利于维护.因此决定自己写一个连接池的开发包.此连接池中主要解决的是提高数据库访问性能,并且尽可能减少连接数目.

说明:

此连接池有三个类和一个接口组成,三个类分别是:

DBConnectionPool 数据库连接池,用户可以通过此类来使用连接池的功能.

PoolConnection 一个实现了java.sql.Connection的warp类,用来和数据库进行通讯.

theOnClose 实现了接口OnConnectionClose的一个类用还处理释放数据库连接的是动作决定是关闭数据库还是返回池中

接口 :

OnConnectionClose:此接口是一个声明,因为本人很不喜欢java的事件机制因此,我经常自己写一些接口来模拟时间,没有java的事件机制那么强大也没有那么复杂.但是效率要比java的事件机制要高那么一点点(笑:).

本来要上传这几个小类的UML图的但是我一点IE就死,所以算了.就只上传代码.还望方家指正.

代码:

package DBTools;

/**

* <p>T数据库连接池工具 </p>

* <p>模拟.NET的连接池,俺一直以为.NET比java优秀 </p>

* <p>Copyright: 可以随便使用,如果有改进最好通知俺</p>

* <p>Company:自己作品 </p>

* @author董平雷

* @version 1.0

*/

import java.sql.*;

import java.util.*;

import java.io.*;

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;

break;

}

}

}

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 , Serializable{

private Connection aCon = null;

private boolean closed = false;

private boolean inUse = false;

private String DriverName;

private OnConnectionClose onClose = null;

private void writeObject(ObjectOutputStream oos) throws IOException {

oos.defaultWriteObject();

}

private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {

ois.defaultReadObject();

}

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);

}

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();

}

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;

}

}

以上就是我所写的连接池代码.

使用方法:

DBTools.DBConnectionPool.SetJDBC("jdbc:mysql://fireBird/trmg?useUnicode=true&characterEncoding=GB2312",

"Administrator","");

DBTools.DBConnectionPool.setDriverName("com.mysql.jdbc.Driver");

java.sql.Connection con = DBTools.DBConnectionPool.getConnection();

当使用完毕了别忘记将con关闭:).

好像现在使用java的人不允许人说java的问题,java的内存回收存在大问题.内存泄漏的厉害,建议如非必要不要使用new来生成新的对象.这样可能可以让我们的系统可以活的更长久一些.还有linux下java性能惨不忍睹,在俺测试的平台中win32反而是最高的.郁闷郁闷不是罪.

2003-9-16 11:06:00

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有