电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

监控Proxool连接池的活动连接变化情况


发布日期:2018/5/16
 

研究了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();来获得一个数据库连接当活动连接数变化时向控制台输出连接信息

上一篇:用Scala实现Qt QWidget对象的Eventable接口

下一篇:类的成员函数的说明