研究了Proxool连接池的源代码后完成下面的程序Proxool连接池的配置过程略
/**//* *@author我为J狂建立日期 * */ packagenetblogjavalzqdiy; importURL; importjavasql*; importorglogicalcobwebsproxoolProxoolException; importorglogicalcobwebsproxoolProxoolFacade; importorglogicanfigurationJAXPConfigurator; importorglogicalcobwebsproxooladminSnapshotIF; publicclassDBConnection { privatestaticbooleaninitialized=false; privatestaticintactiveCount=; publicstaticConnectiongetConnection()throwsSQLException { Connectionconnection=null; if(!initialized) { init(); } connection=DriverManagergetConnection(proxoolDevelop); try { SnapshotIFsnapshot=ProxoolFacadegetSnapshot(Developtrue); intcurActiveCount=snapshotgetActiveConnectionCount();//获得活动连接数 intavailableCount=snapshotgetAvailableConnectionCount();//获得可得到的连接数 intmaxCount=snapshotgetMaximumConnectionCount();//获得总连接数 if(curActiveCount!=activeCount)//当活动连接数变化时输出信息 { Systemoutprintln(); Systemout println(curActiveCount+(active)+availableCount +(available)+maxCount+(max)); Systemoutprintln(); activeCount=curActiveCount; } }catch(ProxoolExceptione) { //TODOAutogeneratedcatchblock eprintStackTrace(); } if(connection!=null) { returnconnection; }else { thrownewNullPointerException( DidntgetconnectionwhichprobablymeansthatnoDriveracceptedtheURL); } } privatestaticvoidinit() { StringfileName=proxoolxml; URLresourceURL=null; try { if(ThreadcurrentThread()getContextClassLoader()!=null) { resourceURL=ThreadcurrentThread()getContextClassLoader() getResource(fileName); } if(resourceURL==null) { resourceURL=DBConnectionclassgetClassLoader()getResource( fileName); } nfigure(resourceURLgetFile()false); ClassforName(orglogicalcobwebsproxoolProxoolDriver); }catch(ClassNotFoundExceptione) { //TODOAutogeneratedcatchblock eprintStackTrace(); }catch(ProxoolExceptione) { //TODOAutogeneratedcatchblock eprintStackTrace(); } initialized=true; } } proxoolxml内容 <?xmlversion=encoding=UTF?> <somethingelseentirely> <proxool> <alias>Develop</alias> <driverurl>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=)(PORT=)))(CONNECT_DATA=(SID=lzqdiy)(SERVER=DEDICATED))) </driverurl> <driverclass>oraclejdbcdriverOracleDriver</driverclass> <driverproperties> <propertyname=uservalue=lzqdiy/> <propertyname=passwordvalue=lzqdiy/> </driverproperties> <maximumconnectioncount></maximumconnectioncount> <minimumconnectioncount></minimumconnectioncount> <housekeepingtestsql>selectCURRENT_DATE</housekeepingtestsql> </proxool> </somethingelseentirely> 通过语句Connection con=DBConnectiongetConnection();来获得一个数据库连接当活动连接数变化时向控制台输出连接信息 |