欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

proxool试用

程序员文章站 2022-05-24 08:48:27
...
配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>DBPool</alias>
<driver-url>jdbc:mysql://127.0.0.1:3306/qianyu</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root" />
<property name="password" value="admin" />
<property name="useUnicode" value="true" />
<property name="characterEncoding" value="utf8" />
</driver-properties>
<!--自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁 -->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!--最少保持的空闲连接数 -->
<prototype-count>5</prototype-count>
<!--允许最大连接数,超过了这个连接,再有请求时,就排在队列中 -->
<maximum-connection-count>1000</maximum-connection-count>
<!--最小连接数 -->
<minimum-connection-count>10</minimum-connection-count>
<!--最大的等待请求数,即因没有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的 用户连接就不会被接受 -->
<proxool.simultaneous-build-throttle>200</proxool.simultaneous-build-throttle>
<!-- 自动重连的,也就是在网络错误或数据库重启等等原因导致与数据库断开,每次获取连接前都会检查,如果现在池子中的连接已经无效将会重新创建 -->
<test-before-use>true</test-before-use>
<!--Test SQL(SELECT getDate()) -->
<house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
</proxool>
</something-else-entirely>

web.xml中加入:
<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>Admin</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.admin.servlet.AdminServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Admin</servlet-name>
<url-pattern>/admin</url-pattern>
</servlet-mapping>
<!-- 配置受保护域,只有Tomcat管理员才能察看连接池的信息 -->
<security-constraint>
<web-resource-collection>
<web-resource-name>proxool</web-resource-name>
<url-pattern>/admin</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>


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();
}
}


重启数据库不影响连接池

上一篇: 堆排序

下一篇: A/B test