proxool是一个开源的连接池,性能优异,最好的是:即时监控连接池状态。
我在tomcat+mysql环境下配置proxool时遇到了不少问题,现总结如下:
1、把proxool库文件放在WEB-INF/lib/,建立WEB-INF/db.xml,内容大概如下:
<?xml version="1.0" encoding="UTF-8" ?>
<proxool>
<alias>xml-test</alias>
<driver-url>jdbc:mysql://localhost/friends?useUnicode=true&characterEncoding=utf-8</driver-url>
<driver-class>org.gjt.mm.mysql.Driver</driver-class>
<statistics>1m,15m,1d</statistics>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</driver-properties>
<maximum-connection-count>100</maximum-connection-count>
<minimum-connection-count>25</minimum-connection-count>
<maximum-active-time>60000</maximum-active-time>
<house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
</proxool>
2、在WEB-INF/web.xml中<web-app>节点里加入如下内容:
<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/db.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>proxool</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>proxool</servlet-name>
<url-pattern>/proxool</url-pattern>
</servlet-mapping>
3、在需要连接数据库时,只需要写:
Connection conn = null;
conn = DriverManager.getConnection("proxool.xml-test");
其它像conn.createStatement();和conn.prepareStatement(sql);之类的操作都和原来一样。
关闭连接一定要注意在catch 和 final里也要关闭