Proxool连接池详细配置

1.先下载Proxool连接池

 地址:http://sourceforge.net/projects/proxool/files/proxool/0.9.1/proxool-0.9.1.zip/download

 

2.将proxool包导入到项目中,proxool-0.9.1.jar、proxool-cglib.jar,(注意:这里还要导入commons-logging.jar包,proxool没有自带这个包

 

3、在WEB-INF下建proxool.xml文件,文件内容如下:



	
		OA
		jdbc:oracle:thin:@127.0.0.1:1521:orcl
		oracle.jdbc.driver.OracleDriver
		
			
			
		
		
		100
		
        10
        
        90000
        
        10
        
        5
        
        true
        
        select sysdate from dual
	

 

这里请注意 如果你拷贝我的代码出现的错误 请手动敲进去 这里可能是编码的问题 或者将中文去掉

 

4.修改WEB.XML,代码如下:

ServletConfigurator  
    
    org.logicalcobwebs.proxool.configuration.ServletConfigurator   
    
    
    xmlFile  
    WEB-INF/proxool.xml  
    
  1  
  
  
  
  Admin  
    
    org.logicalcobwebs.proxool.admin.servlet.AdminServlet   
    
  

 

5.写方法调用,我这里贴出代码,如有错误请指出,谢谢!

 

package com.fp.comm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;
import org.logicalcobwebs.proxool.admin.SnapshotIF;

public class PoolManager {

	private static int activeCount = 0;
	//单例 唯一实例
	private static class PoolManagerInstanse{
		private static final PoolManager instance = new PoolManager();
	}
    
    public static PoolManager getInstance(){
    	return PoolManagerInstanse.instance;
    }
    
    public Connection getConnection(){
    	Connection conn = null;
    	try{
    		Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");  //加载驱动
    		conn = DriverManager.getConnection("proxool.OA"); //此处的DBPool是在proxool.xml中配置的连接池别名 
    		showSnapshotInfo();
    		
    	}catch(Exception e){
    		System.out.println(e.getMessage());
    	}
    	return conn;
    }
    
    /**       
     * 此方法可以得到连接池的信息       
     * showSnapshotInfo       
     */       
    private void showSnapshotInfo(){         
        try{         
            SnapshotIF snapshot = ProxoolFacade.getSnapshot("OA", true);         
            int curActiveCount=snapshot.getActiveConnectionCount();//获得活动连接数         
            int availableCount=snapshot.getAvailableConnectionCount();//获得可得到的连接数         
            int maxCount=snapshot.getMaximumConnectionCount() ;//获得总连接数         
            if(curActiveCount!=activeCount)//当活动连接数变化时输出的信息         
            {         
             System.out.println("活动连接数:"+curActiveCount+"(active)  可得到的连接数:"+availableCount+"(available)  总连接数:"+maxCount+"(max)");                     
             activeCount = curActiveCount;         
            }         
        }catch(ProxoolException e){         
            e.printStackTrace();         
        }         
    }       
    
    /**       
     * 释放连接       
     * freeConnection       
     * @param conn       
     */       
    public static void freeConnection(Connection conn){         
        if(conn != null){         
            try {         
                conn.close();         
            } catch (SQLException e) {                       
                e.printStackTrace();         
            }         
        }         
    }
    
    public static void main(String[] args) {
		PoolManager pool1 = PoolManager.getInstance();
		PoolManager pool2 = PoolManager.getInstance();
		
		if(pool1 == pool2){
			System.out.println(true);
		}else{
			System.out.println(false);
		}
	}
}

 

 

 6。获取连接的方法

 

 Connection conn = PoolManager.getInstance().getConnection();

你可能感兴趣的:(数据库)