proxool连接池配置详细说明

转载至: http://blog.csdn.net/hjjzhangkui/archive/2010/10/22/5958832.aspx
目前市面上三个主流连接池从性能上排名如下:proxool>c3p0>dbcp,proxool还提供了可视化的连接池实时监控工具,所以既稳定又方便,配置也是非常容易的事情。下面我来讲讲我如何配置proxool连接池的。
      1、下载相关资源。

      从http://proxool.sourceforge.net/站点上下载我们需要的资源,proxool-0.9.1是最新版本,解压后从lib下copy里面的2个JAR包 proxool-0.9.1.jar和 

proxool-cglib.jar至项目中去。如果不加proxool-cglib.jar的话会报 java.lang.ClassNotFoundException:org.logicalcobwebs.cglib.proxy.Callback异常。

      2、首先在WEB-INF目录下新建一个proxool.xml文件。

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>  
<proxool-config>  
    <proxool>  
        <alias>datasource1</alias>  
        <driver-url>jdbc:mysql://localhost:3306/datasource1</driver-url>  
        <driver-class>com.mysql.jdbc.Driver</driver-class>  
        <driver-properties>  
            <property name="user" value="root" />  
            <property name="password" value="root" />  
        </driver-properties>  
        <house-keeping-sleep-time>90000</house-keeping-sleep-time>  
        <maximum-new-connections>20</maximum-new-connections>  
        <prototype-count>5</prototype-count>  
        <maximum-connection-count>1000</maximum-connection-count>  
        <minimum-connection-count>10</minimum-connection-count>  
    </proxool>  
      
    <proxool>  
        <alias>datasource2</alias>  
        <driver-url>jdbc:sybase:Tds:localhost:16428/datasource2</driver-url>  
        <driver-class>com.sybase.jdbc3.jdbc.SybDriver</driver-class>  
        <driver-properties>  
            <property name="user" value="dba" />  
            <property name="password" value="dba" />  
        </driver-properties>  
        <house-keeping-sleep-time>90000</house-keeping-sleep-time>  
        <maximum-new-connections>20</maximum-new-connections>  
        <prototype-count>5</prototype-count>  
        <maximum-connection-count>1000</maximum-connection-count>  
        <minimum-connection-count>10</minimum-connection-count>  
    </proxool>  
</proxool-config> 
<?xml version="1.0" encoding="UTF-8"?>
<proxool-config>
<proxool>
<alias>datasource1</alias>
<driver-url>jdbc:mysql://localhost:3306/datasource1</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root" />
<property name="password" value="root" />
</driver-properties>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<maximum-new-connections>20</maximum-new-connections>
<prototype-count>5</prototype-count>
<maximum-connection-count>1000</maximum-connection-count>
<minimum-connection-count>10</minimum-connection-count>
</proxool>

<proxool>
<alias>datasource2</alias>
<driver-url>jdbc:sybase:Tds:localhost:16428/datasource2</driver-url>
<driver-class>com.sybase.jdbc3.jdbc.SybDriver</driver-class>
<driver-properties>
<property name="user" value="dba" />
<property name="password" value="dba" />
</driver-properties>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<maximum-new-connections>20</maximum-new-connections>
<prototype-count>5</prototype-count>
<maximum-connection-count>1000</maximum-connection-count>
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</proxool-config>

我在里面配置了2个不同数据库的数据源,你可以根据需要配置多个,其中具体的参数属性我会在下面全部予以说明。
      3、配置web.xml文件。

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8" ?>  
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
    <servlet>  
        <servlet-name>ServletConfigurator</servlet-name>  
        <servlet-class>  
            org.logicalcobwebs.proxool.configuration.ServletConfigurator  
        </servlet-class>  
        <init-param>  
            <param-name>xmlFile</param-name>  
            <param-value>WEB-INF/proxool.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet>  
        <servlet-name>datasource_situation</servlet-name>  
        <servlet-class>  
            org.logicalcobwebs.proxool.admin.servlet.AdminServlet  
        </servlet-class>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>datasource_situation</servlet-name>  
        <url-pattern>/datasource_situation</url-pattern>  
    </servlet-mapping>  
    <!-- 配置受保护域,只有Tomcat管理员才能察看连接池的信息 -->  
    <security-constraint>  
        <web-resource-collection>  
            <web-resource-name>proxool</web-resource-name>  
            <url-pattern>/datasource_situation</url-pattern>  
        </web-resource-collection>  
        <auth-constraint>  
            <role-name>manager</role-name>  
        </auth-constraint>  
    </security-constraint>  
    <login-config>  
        <auth-method>BASIC</auth-method>  
        <realm-name>proxool manager Application</realm-name>  
    </login-config>  
    <security-role>  
        <description>  
            The role that is required to log in to the Manager  
            Application  
        </description>  
        <role-name>manager</role-name>  
    </security-role>  
    <error-page>  
        <error-code>401</error-code>  
        <location>/401.jsp</location>  
    </error-page>  
</web-app> 
<?xml version="1.0" encoding="UTF-8" ?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.configuration.ServletConfigurator
</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>WEB-INF/proxool.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>datasource_situation</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.admin.servlet.AdminServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>datasource_situation</servlet-name>
<url-pattern>/datasource_situation</url-pattern>
</servlet-mapping>
<!-- 配置受保护域,只有Tomcat管理员才能察看连接池的信息 -->
<security-constraint>
<web-resource-collection>
<web-resource-name>proxool</web-resource-name>
<url-pattern>/datasource_situation</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>manager</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>proxool manager Application</realm-name>
</login-config>
<security-role>
<description>
The role that is required to log in to the Manager
Application
</description>
<role-name>manager</role-name>
</security-role>
<error-page>
<error-code>401</error-code>
<location>/401.jsp</location>
</error-page>
</web-app>

<load-on-startup>1</load-on-startup>用来设置加载属性,一定要保证在其他配置项之前加载。

view plaincopy to clipboardprint?
<servlet>  
    <servlet-name>datasource_situation</servlet-name>  
    <servlet-class>  
        org.logicalcobwebs.proxool.admin.servlet.AdminServlet  
    </servlet-class>  
</servlet>  
<servlet-mapping>  
    <servlet-name>datasource_situation</servlet-name>  
    <url-pattern>/datasource_situation</url-pattern>  
</servlet-mapping> 
<servlet>
<servlet-name>datasource_situation</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.admin.servlet.AdminServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>datasource_situation</servlet-name>
<url-pattern>/datasource_situation</url-pattern>
</servlet-mapping>

这个属性是用来提供察看连接池信息的工具,既在域名后跟上/datasource_situation就可以查看了,当然名字可以自己定义。如果你想限制用户和密码来访问就用

view plaincopy to clipboardprint?
<security-constraint>  
        <web-resource-collection>  
            <web-resource-name>proxool</web-resource-name>  
            <url-pattern>/datasource_situation</url-pattern>  
        </web-resource-collection>  
        <auth-constraint>  
            <role-name>manager</role-name>  
        </auth-constraint>  
    </security-constraint>  
    <login-config>  
        <auth-method>BASIC</auth-method>  
        <realm-name>proxool manager Application</realm-name>  
    </login-config>  
    <security-role>  
        <description>  
            The role that is required to log in to the Manager  
            Application  
        </description>  
        <role-name>manager</role-name>  
    </security-role>  
    <error-page>  
        <error-code>401</error-code>  
        <location>/401.jsp</location>  
    </error-page> 
<security-constraint>
<web-resource-collection>
<web-resource-name>proxool</web-resource-name>
<url-pattern>/datasource_situation</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>manager</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>proxool manager Application</realm-name>
</login-config>
<security-role>
<description>
The role that is required to log in to the Manager
Application
</description>
<role-name>manager</role-name>
</security-role>
<error-page>
<error-code>401</error-code>
<location>/401.jsp</location>
</error-page>

这段配置就限定了只有输入了TOMCAT的管理员帐号密码后就能登陆连接池查看工具了,如果连续三次登陆失败,进入401.jsp页面。401.jsp要加入下段代码

           response.setHeader("WWW-Authenticate", "Basic realm=\"Tomcat Manager Application\"");

      4、配置连接池连接类。

view plaincopy to clipboardprint?
package selfservice;          
         
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;          
         
         
public class PoolManager {          
              
    private static int activeCount = 0;          
              
              
    public PoolManager(){          
                  
    }             
    /**       
     * 获取连接       
     * getConnection       
     * @param name       
     * @return       
     */         
    public Connection getConnection() {          
        try{          
            Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");//proxool驱动类          
            Connection conn = DriverManager.getConnection("proxool.datasource1");       
           //此处的datasource1是在proxool.xml中配置的连接池别名,当然根据需要可以用datasource2         
            showSnapshotInfo();          
                      
            return conn;          
        }catch(Exception ex){          
            ex.printStackTrace();          
        }          
        return null;          
    }          
    /**       
     * 此方法可以得到连接池的信息       
     * showSnapshotInfo       
     */         
    private void showSnapshotInfo(){          
        try{          
            SnapshotIF snapshot = ProxoolFacade.getSnapshot("datasource1", 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();          
        }          
    }          
    /**       
     * 获取连接       
     * 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 * from tblgxinterface");          
                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();          
        }          
         
    }          
         
}   
package selfservice;       
      
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;       
      
      
public class PoolManager {       
           
    private static int activeCount = 0;       
           
           
    public PoolManager(){       
               
    }          
    /**     
     * 获取连接     
     * getConnection     
     * @param name     
     * @return     
     */      
    public Connection getConnection() {       
        try{       
            Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");//proxool驱动类       
            Connection conn = DriverManager.getConnection("proxool.datasource1");    
           //此处的datasource1是在proxool.xml中配置的连接池别名,当然根据需要可以用datasource2      
            showSnapshotInfo();       
                   
            return conn;       
        }catch(Exception ex){       
            ex.printStackTrace();       
        }       
        return null;       
    }       
    /**     
     * 此方法可以得到连接池的信息     
     * showSnapshotInfo     
     */      
    private void showSnapshotInfo(){       
        try{       
            SnapshotIF snapshot = ProxoolFacade.getSnapshot("datasource1", 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();       
        }       
    }       
    /**     
     * 获取连接     
     * 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 * from tblgxinterface");       
                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();       
        }       
      
    }       
      
}  

下面我来介绍一下proxool.xml文件中关于proxool各个属性的详细说明:

fatal-sql-exception: 它是一个逗号分割的信息片段.当一个SQL异常发生时,他的异常信息将与这个信息片段进行比较.如果在片段中存在,那么这个异常将被认为是个致命错误(Fatal SQL Exception ).这种情况下,数据库连接将要被放弃.无论发生什么,这个异常将会被重掷以提供给消费者.用户最好自己配置一个不同的异常来抛出.





fatal-sql-exception-wrapper-class:正如上面所说,你最好配置一个不同的异常来重掷.利用这个属性,用户可以包装SQLException,使他变成另外一个异常.这个异常或者继承SQLException或者继承字RuntimeException.proxool自带了2个实现:'org.logicalcobwebs.proxool.FatalSQLException' 和'org.logicalcobwebs.proxool.FatalRuntimeException' .后者更合适.





house-keeping-sleep-time: house keeper 保留线程处于睡眠状态的最长时间,house keeper 的职责就是检查各个连接的状态,并判断是否需要销毁或者创建.





house-keeping-test-sql:  如果发现了空闲的数据库连接.house keeper 将会用这个语句来测试.这个语句最好非常快的被执行.如果没有定义,测试过程将会被忽略。





injectable-connection-interface: 允许proxool实现被代理的connection对象的方法.





injectable-statement-interface: 允许proxool实现被代理的Statement 对象方法.





injectable-prepared-statement-interface: 允许proxool实现被代理的PreparedStatement 对象方法.





injectable-callable-statement-interface: 允许proxool实现被代理的CallableStatement 对象方法.





jmx: 略






jmx-agent-id: 略





jndi-name: 数据源的名称





maximum-active-time: 如果housekeeper 检测到某个线程的活动时间大于这个数值.它将会杀掉这个线程.所以确认一下你的服务器的带宽.然后定一个合适的值.默认是5分钟.





maximum-connection-count: 最大的数据库连接数.





maximum-connection-lifetime: 连接最大生命时间 默认4小时





minimum-connection-count: 最小的数据库连接数





overload-without-refusal-lifetime: 略





prototype-count: 连接池中可用的连接数量.如果当前的连接池中的连接少于这个数值.新的连接将被建立(假设没有超过最大可用数).例如.我们有3个活动连接2个可用连接,而我们的prototype-count是4,那么数据库连接池将试图建立另外2个连接.这和 minimum-connection-count不同. minimum-connection-count把活动的连接也计算在内.prototype-count 是spare connections 的数量.





recently-started-threshold:  略





simultaneous-build-throttle:  同时最大连接数





statistics:  连接池使用状况统计。 参数“10s,1m,1d”




statistics-log-level:  日志统计跟踪类型。 参数“ERROR”或 “INFO”




test-before-use: 略




test-after-use: 略




trace: 如果为true,那么每个被执行的SQL语句将会在执行期被log记录(DEBUG LEVEL).你也可以注册一个ConnectionListener (参看ProxoolFacade)得到这些信息.




verbose: 详细信息设置。 参数 bool 值



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hjjzhangkui/archive/2010/10/22/5958832.aspx

你可能感兴趣的:(sql,mysql,活动,配置管理,Sybase)