监控Proxool连接池的活动连接变化情况
2008-01-05 20:21:27 来源:WEB开发网核心提示:研究了PRoxool连接池的源代码后完成下面的程序,Proxool连接池的配置过程略,监控Proxool连接池的活动连接变化情况,/**//**@author 我为J狂 建立日期 2007-4-18**/package net.blogjava.lzqdiy;import java.net.URL;import jav
研究了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;
}
}
更多精彩
赞助商链接