作者 : manboo
.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反而是最高的.郁闷郁闷不是罪.
闽公网安备 35060202000074号