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

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

2007-4-23     推荐:973    收藏:628    评论:0     来源: e800Java频道

研究了Proxool连接池的源代码后完成下面的程序,Proxool连接池的配置过程略。


/**/ /*
*@author 我为J狂 建立日期 2007-4-18
*
*/

package net.blogjava.lzqdiy;

import java.net.URL;
import java.sql. * ;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
import org.logicalcobwebs.proxool.admin.SnapshotIF;

public class DBConnection
{

private static boolean initialized = false;

private static int activeCount = 0;

public static Connection getConnection() throws SQLException
{
Connection connection
= null;
if (!initialized)
{

init();
}


connection
= DriverManager.getConnection("proxool.Develop");
try
{
SnapshotIF snapshot
= ProxoolFacade.getSnapshot("Develop", true);
int curActiveCount = snapshot.getActiveConnectionCount();// 获得活动连接数
int availableCount = snapshot.getAvailableConnectionCount();// 获得可得到的连接数
int maxCount = snapshot.getMaximumConnectionCount();// 获得总连接数
if (curActiveCount != activeCount)// 当活动连接数变化时输出信息
{
System.out.println(
"----------------------------------");
System.out
.println(curActiveCount
+ "(active) " + availableCount
+ "(available) " + maxCount + "(max)");
System.out.println(
"----------------------------------");
activeCount
= curActiveCount;
}


}
catch (ProxoolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

if (connection != null)
{

return connection;
}
else
{
throw new NullPointerException(
"Didn''t get connection, which probably means that no Driver accepted the URL");
}


}


private static void init()
{
String fileName
= "proxool.xml";
URL resourceURL
= null;
try
{
if (Thread.currentThread().getContextClassLoader() != null)
{
resourceURL
= Thread.currentThread().getContextClassLoader()
.getResource(fileName);
}


if (resourceURL == null)
{
resourceURL
= DBConnection.class.getClassLoader().getResource(
fileName);
}


JAXPConfigurator.configure(resourceURL.getFile(),
false);
Class.forName(
"org.logicalcobwebs.proxool.ProxoolDriver");
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ProxoolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

initialized
= true;
}

}

proxool.xml内容

xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>Develop<!---->alias>
<driver-url>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.6)(PORT=1521)))(CONNECT_DATA=(SID=lzqdiy)(SERVER=DEDICATED)))
<!---->driver-url>
<driver-class>oracle.jdbc.driver.OracleDriver<!---->driver-class>
<driver-properties>
<property name="user" value="lzqdiy"/>
<property name="password" value="lzqdiy"/>
<!---->driver-properties>
<maximum-connection-count>120<!---->maximum-connection-count>
<minimum-connection-count>20<!---->minimum-connection-count>
<house-keeping-test-sql>select CURRENT_DATE<!---->house-keeping-test-sql>
<!---->proxool>
<!---->something-else-entirely>

通过语句:Connection con=DBConnection.getConnection();来获得一个数据库连接,当活动连接数变化时向控制台输出连接信息。

你可能感兴趣的:(oracle,sql,sql,jdbc,server,活动)