proxool代码创建数据库连接与获取监控状态
package com.originalsoft.Test; 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; /** * @author tank * @date:Apr 15, 2013 6:14:08 PM * @description: * @version : */ public class DBConnection { private static boolean initialized = false; private static int activeCount = 0; public static void state() throws ProxoolException{ SnapshotIF snapshot = ProxoolFacade.getSnapshot("mySqlProxool", 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; // } } public static Connection getConnection() throws SQLException { Connection connection = null; if (!initialized) { init(); } connection = DriverManager.getConnection("proxool.mySqlProxool"); try { SnapshotIF snapshot = ProxoolFacade.getSnapshot("mySqlProxool", 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; } }
注:
mySqlProxool为注册proxool的别名
<?xml version="1.0" encoding="UTF-8"?> <something-else-entirely> <proxool> <alias>mySqlProxool</alias> <driver-url>jdbc:mysql://127.0.0.1:3306/db</driver-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <driver-properties> <property name="user" value="XXX" /> <property name="password" value="XXX" /> <property name="autoReconnect" value="true" /> </driver-properties> <!-- <jndi-name>jdbc-mahout</jndi-name> <jndi-java.naming.factory.initial> org.apache.naming.java.javaURLContextFactory </jndi-java.naming.factory.initial> <jndi-java.naming.factory.url.pkgs>org.apache.naming</jndi-java.naming.factory.url.pkgs> --> <test-before-use>true</test-before-use> <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql> <house-keeping-sleep-time>90000</house-keeping-sleep-time> <simultaneous-build-throttle>100</simultaneous-build-throttle><!-- old name maximum-new-connections --> <maximum-connection-count>300</maximum-connection-count> <minimum-connection-count>20</minimum-connection-count> <!-- <maximum-connection-lifetime>3600000</maximum-connection-lifetime> <maximum-active-time>120000</maximum-active-time>--> <prototype-count>5</prototype-count><!-- mini keep connection --> <maximum-active-time>1200000</maximum-active-time> </proxool> </something-else-entirely>