Chinaunix首页 | 论坛 | 博客
  • 博客访问: 499981
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 13:26:32

/*
*@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;
    }

}


--------------------next---------------------
xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
  
<proxool>
    
<alias>Developalias>
    
<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.Driverdriver-class>
    
<driver-properties>
      
<property name="user" value="lzqdiy"/>
      
<property name="password" value="lzqdiy"/>
    
driver-properties>
    
<maximum-connection-count>120maximum-connection-count>
    
<minimum-connection-count>20minimum-connection-count>
    
<house-keeping-test-sql>select CURRENT_DATEhouse-keeping-test-sql>
  
proxool>
something-else-entirely>


--------------------next---------------------

阅读(196) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~