proxool试用

阅读更多
配置文件:


	
		DBPool
		jdbc:mysql://127.0.0.1:3306/qianyu
		com.mysql.jdbc.Driver
		
			
			
			
			
		
		
		90000
		
		5
		
		1000
		
		10
		
		200
		
		true
		
		select CURRENT_DATE
	


web.xml中加入:
  
  ServletConfigurator  
    
    org.logicalcobwebs.proxool.configuration.ServletConfigurator  
    
    
    xmlFile  
    WEB-INF/proxool.xml  
    
  1  
  
  
  
  Admin  
    
    org.logicalcobwebs.proxool.admin.servlet.AdminServlet  
    
  
  
  Admin  
  /admin  
  
  
  
    
      proxool   
      /admin  
    
    
     manager   
       
    
   
     BASIC   
     proxool manager Application   
    
    
    The role that is required to log in to the Manager Application   
     manager   
  


java测试代码:
/**   
 * java (__FILE__, "$Revision: 1.1 $")
 *
 * @ A.java Create on 2014-3-26   
 *
 * Copyright (c) 2014 by [email protected].   
 */  
package com.dingding.dao;



import java.sql.Connection;       
import java.sql.DriverManager;       
import java.sql.ResultSet;       
import java.sql.SQLException;       
import java.sql.Statement;       
      
import org.logicalcobwebs.proxool.ProxoolException;       
import org.logicalcobwebs.proxool.ProxoolFacade;       
import org.logicalcobwebs.proxool.admin.SnapshotIF;       

import com.pachira.utils.SrvLogger;
      
      
public class ProxoolPoolManager {       
           
    private static int activeCount = 0;       
           
           
    public ProxoolPoolManager(){       
               
    }          
    /**     
     * 获取连接     
     * getConnection     
     * @param name     
     * @return     
     */      
    public Connection getConnection() {       
        try{       
            Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");//proxool驱动类       
            Connection conn = DriverManager.getConnection("proxool.DBPool");    
           //此处的DBPool是在proxool.xml中配置的连接池别名      
            showSnapshotInfo();       
                   
            return conn;       
        }catch(Exception ex){       
            ex.printStackTrace();       
        }       
        return null;       
    }       
    /**     
     * 此方法可以得到连接池的信息     
     * showSnapshotInfo     
     */      
    private void showSnapshotInfo(){       
        try{       
            SnapshotIF snapshot = ProxoolFacade.getSnapshot("DBPool", true);       
            int curActiveCount=snapshot.getActiveConnectionCount();//获得活动连接数       
            int availableCount=snapshot.getAvailableConnectionCount();//获得可得到的连接数       
            int maxCount=snapshot.getMaximumConnectionCount() ;//获得总连接数       
            if(curActiveCount!=activeCount)//当活动连接数变化时输出的信息       
            {       
             SrvLogger.debug(this.getClass(),"活动连接数:"+curActiveCount+"(active)  可得到的连接数:"+availableCount+"(available)  总连接数:"+maxCount+"(max)");                    
             activeCount=curActiveCount;       
            }       
        }catch(ProxoolException e){       
            SrvLogger.errorW(getClass(), "showSnapshotInfo error,",e);   
        }       
    }       
    /**     
     * 获取连接     
     * getConnection     
     * @param name     
     * @return     
     */      
    public Connection getConnection(String name){       
        return getConnection();       
    }       
    /**     
     * 释放连接     
     * freeConnection     
     * @param conn     
     */      
    public void freeConnection(Connection conn){       
        if(conn!=null){       
            try {       
                conn.close();       
            } catch (SQLException e) {                     
                e.printStackTrace();       
            }       
        }       
    }       
    /**     
     * 释放连接     
     * freeConnection     
     * @param name     
     * @param con     
     */      
    public void freeConnection (String name,Connection con){       
        freeConnection(con);       
    }       
           
    public void getQuery() {               
        try {       
            Connection conn = getConnection();       
            if(conn != null){       
                Statement statement = conn.createStatement();       
                ResultSet rs = statement.executeQuery("select id,uuid from Transcription");       
                int c = rs.getMetaData().getColumnCount();       
                while(rs.next()){                          
                    System.out.println();       
                    for(int i=1;i<=c;i++){       
                        System.out.print(rs.getObject(i));       
                    }       
                }       
                rs.close();       
            }       
            freeConnection(conn);       
        } catch (SQLException e) {                 
            e.printStackTrace();       
        }       
      
    }       
      
    
    public static void main(String[] args) {
    	ProxoolPoolManager pool=new ProxoolPoolManager();
    	pool.getQuery();
	}
}


重启数据库不影响连接池

你可能感兴趣的:(proxool试用)