---
title: "spring中配置proxool数据源"
author: "spiritfrog"
date: "2010-07-09T12:49:43Z"
tags: ["JDBC", "SQL", "Servlet", "Spring", "配置管理"]
categories: ["企业架构"]
slug: "spring-proxool"
draft: false
lastmod: "2010-07-09T13:01:29Z"
id: 709043
url: "https://spiritfrog.iteye.com/blog/709043"
---

方式一：  
 在Spring的"applicationContext.xml"中的dataSource bean定义——

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;bean id="dataSource"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    class="org.logicalcobwebs.proxool.ProxoolDataSource"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="driver"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;value&gt;com.mysql.jdbc.Driver&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="driverUrl"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;value&gt;jdbc:mysql://localhost:3306/dbname?user=yourname&amp;password=yourpass&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="user" value="yourname" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="password" value="yourpass" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="alias" value="Pool\_dbname" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="houseKeepingSleepTime" value="90000" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="prototypeCount" value="0" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="maximumConnectionCount" value="50" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="minimumConnectionCount" value="2" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="simultaneousBuildThrottle" value="50" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="maximumConnectionLifetime" value="14400000" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="houseKeepingTestSql" value="select CURRENT\_DATE" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/bean&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

第一种方式需要把用户名和密码写在连接串里面，  
ProxoolDataSource类提供的user，password属性似乎没有什么用。  
**无论提供什么，它都会以空用户名、密码去连接数据库。**  
**这可能是Proxool RC0.93的一个bug，实在让人恼火，不知道最新的0.9.1有没有fix这个bug。**  
不过配置中的user，password两个属性还必须设置，否则hibernate会报空指针错误。

方式二：  
 在Spring的"applicationContext.xml"中的dataSource bean定义——

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;bean id="dataSource"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="driverClassName"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;value&gt;org.logicalcobwebs.proxool.ProxoolDriver&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="url"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;value&gt;proxool.Pool\_dbname&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/bean&gt;

第二种方式需要预先在"web.xml"先配置好Proxool连接池，配置如下：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;servlet&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;servlet-name&gt;proxoolServletConfigurator&lt;/servlet-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;servlet-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        org.logicalcobwebs.proxool.configuration.ServletConfigurator  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/servlet-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;init-param&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;param-name&gt;xmlFile&lt;/param-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;param-value&gt;WEB-INF/proxool.xml&lt;/param-value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/init-param&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/servlet&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;servlet&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;servlet-name&gt;context&lt;/servlet-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;servlet-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        org.springframework.web.context.ContextLoaderServlet  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/servlet-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/servlet&gt;

**注意，第二种方式下Spring的上下文加载如果想使用listener方式(Struts2要求)，**  
**则与连接池有关的Bean全得延迟初始化。因为listener比servlet优先初始化，**  
**如果相关Bean不是lazy-init的话，则启动服务器时会出现Bean找不到连接定义的异常。**

 &lt;listener&gt;  
  &lt;listener-class&gt;  
      org.springframework.web.context.ContextLoaderListener  
  &lt;/listener-class&gt;  
 &lt;/listener&gt;

其中proxool的配置文件可以采用xmlFile"proxool.xml"或者propertyFile"proxool.properties"

"proxool.xml"格式如下：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;proxool-config&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;proxool&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;alias&gt;Pool\_dbname&lt;/alias&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;driver-url&gt;jdbc:mysql://localhost:3306/dbname&lt;/driver-url&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;driver-class&gt;com.mysql.jdbc.Driver&lt;/driver-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;driver-properties&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;property name="user" value="yourname"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;property name="password" value="yourpass"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;/driver-properties&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;house-keeping-sleep-time&gt;60000&lt;/house-keeping-sleep-time&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;maximum-connection-count&gt;20&lt;/maximum-connection-count&gt;　  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;minimum-connection-count&gt;2&lt;/minimum-connection-count&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;prototype-count&gt;0&lt;/prototype-count&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;simultaneous-build-throttle&gt;20&lt;/simultaneous-build-throttle&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;house-keeping-test-sql&gt;select CURRENT\_DATE&lt;/house-keeping-test-sql&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;statistics&gt;15s,10m,1d&lt;/statistics&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;statistics-log-level&gt;INFO&lt;/statistics-log-level&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/proxool&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;proxool&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;!--可以配置多个库--&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/proxool&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/proxool-config&gt;

"proxool.properties"格式如下：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.alias=Pool\_dbname  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.driver-url=jdbc:mysql://localhost:3306/dbname  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.driver-class=com.mysql.jdbc.Driver  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.user=yourname  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.password=yourpass  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.house-keeping-sleep-time=60000  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.house-keeping-test-sql=select CURRENT\_DATE  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.maximum-connection-count=10  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.minimum-connection-count=3  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.maximum-connection-lifetime=18000000  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.prototype-count=3  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.simultaneous-build-throttle=10  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.recently-started-threshold=60000  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.overload-without-refusal-lifetime=50000  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.maximum-active-time=60000  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.verbose=true  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.trace=true  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-0.proxool.fatal-sql-exception=Fatal error  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc-2.proxool.alias=Pool\_dbname2  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)……  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!--可以配置多个库--&gt;

**三、更详细的proxool.xml的配置属性说明:**   
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)

```
<?xml version="1.0" encoding="ISO-8859-1"?><!--
Properties for Proxool Configurator testing. Defines the same parameters as
TestHelper.buildCompleteAlternativeProperties()
-->
<something-else-entirely xmlns="http://sumthin.else.entirely" xmlns:proxool="The latest version is available at http://proxool.sourceforge.net/xml-namespace">
    <proxool:proxool>
        <proxool:alias>xml-test-ns</proxool:alias>
        <proxool:driver-url>jdbc:hsqldb:db/test</proxool:driver-url>
        <proxool:driver-class>org.hsqldb.jdbcDriver</proxool:driver-class>
        <proxool:driver-properties>
            <proxool:property name="user" value="sa"/>
            <proxool:property name="password" value=""/>
        </proxool:driver-properties>
        <proxool:house-keeping-sleep-time>40000</proxool:house-keeping-sleep-time>
        <proxool:house-keeping-test-sql>select CURRENT_DATE</proxool:house-keeping-test-sql>
        <proxool:maximum-connection-count>10</proxool:maximum-connection-count>
        <proxool:minimum-connection-count>3</proxool:minimum-connection-count>
        <proxool:maximum-connection-lifetime>18000000</proxool:maximum-connection-lifetime> <!-- 5 hours -->
        <proxool:simultaneous-build-throttle>5</proxool:simultaneous-build-throttle>
        <proxool:recently-started-threshold>40000</proxool:recently-started-threshold>
        <proxool:overload-without-refusal-lifetime>50000</proxool:overload-without-refusal-lifetime>
        <proxool:maximum-active-time>60000</proxool:maximum-active-time>
        <proxool:verbose>true</proxool:verbose>
        <proxool:trace>true</proxool:trace>
        <proxool:fatal-sql-exception>Fatal error</proxool:fatal-sql-exception>
        <proxool:prototype-count>2</proxool:prototype-count>
    </proxool:proxool>
    <nothing-to-do-with-proxool>
        <proxool:proxool>
            <proxool:alias>xml-test-ns-2</proxool:alias>
            <proxool:driver-url>jdbc:hsqldb:db/test</proxool:driver-url>
            <proxool:driver-class>org.hsqldb.jdbcDriver</proxool:driver-class>
            <proxool:driver-properties>
                <proxool:property name="user" value="sa"/>
                <proxool:property name="password" value=""/>
            </proxool:driver-properties>
            <proxool:house-keeping-sleep-time>40000</proxool:house-keeping-sleep-time>
            <proxool:house-keeping-test-sql>select CURRENT_DATE</proxool:house-keeping-test-sql>
            <proxool:maximum-connection-count>10</proxool:maximum-connection-count>
            <proxool:minimum-connection-count>3</proxool:minimum-connection-count>
            <proxool:maximum-connection-lifetime>18000000</proxool:maximum-connection-lifetime> <!-- 5 hours -->
            <proxool:simultaneous-build-throttle>5</proxool:simultaneous-build-throttle>
            <proxool:recently-started-threshold>40000</proxool:recently-started-threshold>
            <proxool:overload-without-refusal-lifetime>50000</proxool:overload-without-refusal-lifetime>
            <proxool:maximum-active-time>60000</proxool:maximum-active-time>
            <proxool:verbose>true</proxool:verbose>
            <proxool:trace>true</proxool:trace>
            <proxool:fatal-sql-exception>Fatal error</proxool:fatal-sql-exception>
            <proxool:prototype-count>2</proxool:prototype-count>
        </proxool:proxool>
    </nothing-to-do-with-proxool>
</something-else-entirely>
```

**属性列表说明:**

**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:** 一个线程的最大寿命.

**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 值   

 

四、还可以配置管理proxool的servlet

```
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>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>
  <error-page>
    <error-code>401</error-code>
    <location>/401.jsp</location>
  </error-page>
</web-app>
```