熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

監控Proxool連接池的活動連接變化情況

2013-11-23 18:49:25  來源: Java核心技術 

  研究了Proxool連接池的源代碼後完成下面的程序Proxool連接池的配置過程略



/**//*
*@author 我為J狂 建立日期 
*
*/
package netblogjavalzqdiy;

import URL;
import javasql*;
import orglogicalcobwebsproxoolProxoolException;
import orglogicalcobwebsproxoolProxoolFacade;
import orglogicanfigurationJAXPConfigurator;
import orglogicalcobwebsproxooladminSnapshotIF;

public class DBConnection
{

    private static boolean initialized = false;

    private static int activeCount = ;

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

            init();
        }

        connection = DriverManagergetConnection(proxoolDevelop);
        try
        {
            SnapshotIF snapshot = ProxoolFacadegetSnapshot(Develop true);
            int curActiveCount = snapshotgetActiveConnectionCount();// 獲得活動連接數
            int availableCount = snapshotgetAvailableConnectionCount();// 獲得可得到的連接數
            int maxCount = snapshotgetMaximumConnectionCount();// 獲得總連接數
            if (curActiveCount != activeCount)// 當活動連接數變化時輸出信息
            {
                Systemoutprintln();
                Systemout
                        println(curActiveCount + (active)   + availableCount
                                + (available)   + maxCount + (max));
                Systemoutprintln();
                activeCount = curActiveCount;
            }

        } catch (ProxoolException e)
        {
            // TODO Autogenerated catch block
            eprintStackTrace();
        }
        if (connection != null)
        {

            return connection;
        } else
        {
            throw new NullPointerException(
                    Didnt get connection which probably means that no Driver accepted the URL);
        }

    }

    private static void init()
    {
        String fileName = proxoolxml;
        URL resourceURL = null;
        try
        {
            if (ThreadcurrentThread()getContextClassLoader() != null)
            {
                resourceURL = ThreadcurrentThread()getContextClassLoader()
                        getResource(fileName);
            }

            if (resourceURL == null)
            {
                resourceURL = DBConnectionclassgetClassLoader()getResource(
                        fileName);
            }

            nfigure(resourceURLgetFile() false);
            ClassforName(orglogicalcobwebsproxoolProxoolDriver);
        } catch (ClassNotFoundException e)
        {
            // TODO Autogenerated catch block
            eprintStackTrace();
        } catch (ProxoolException e)
        {
            // TODO Autogenerated catch block
            eprintStackTrace();
        }
        initialized = true;
    }
}

  proxoolxml內容


<?xml version= 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>
      <property name=user value=lzqdiy/>
      <property name=password value=lzqdiy/>
    </driverproperties>
    <maximumconnectioncount></maximumconnectioncount>
    <minimumconnectioncount></minimumconnectioncount>
    <housekeepingtestsql>select CURRENT_DATE</housekeepingtestsql>
  </proxool>
</somethingelseentirely>

通過語句Connection con=DBConnectiongetConnection();來獲得一個數據庫連接當活動連接數變化時向控制台輸出連接信息
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25800.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.