Java Web中使用数据库连接池proxool配置详解
使用proxool数据库连接池步骤:
在WEB-INF的lib中导入proxool-0.9.0RC3.jar
在WEB-INF中新建一个XML文件,取名为proxool.xml
在proxool.xml中的内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <!--
- the proxool configuration can be embedded within your own application's. Anything outside the "proxool"
- tag is ignored.
- -->
- <something-else-entirely>
- <proxool>
- <alias>smms</alias> <!--数据源的别名-->
- <driver-url>jdbc:mysql://127.0.0.1:3306/smms</driver-url><!--url连接串-->
- <driver-class>com.mysql.jdbc.Driver</driver-class><!--驱动类-->
- <driver-properties>
- <property name="user" value="root" /><!--用户名-->
- <property name="password" value="root" /><!--密码-->
- </driver-properties>
- <!--最大连接数(默认 5个),超过了这个连接数,再有请求时,就排在队列中等候,最大的等待请求数由
- maximum-new-connections决定-->
- <maximum-connection-count>100</maximum-connection-count>
- <!--最小连接数(默认2个)-->
- <minimum-connection-count>10</minimum-connection-count>
- <!--proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁 默
- 认30秒-->
- <house-keeping-sleep-time>90000</house-keeping-sleep-time>
- <!--没有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的用户连接就不会被接受-
- ->
- <maximum-new-connections>10</maximum-new-connections>
- <!--最少保持的空闲连接数(默认2个)-->
- <prototype-count>5</prototype-count>
- <!--在使用之前测试-->
- <test-before-use>true</test-before-use>
- <!--用于保持连接的测试语句 -->
- <house-keeping-test-sql>select id from t_sysadmin</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>
- t;/servlet>
数据库操作类如下:
- package cn.edu.hbcit.smms.dao.databasedao;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.apache.log4j.Logger;
- import org.logicalcobwebs.proxool.ProxoolException;
- import org.logicalcobwebs.proxool.ProxoolFacade;
- import org.logicalcobwebs.proxool.admin.SnapshotIF;
- /**
- * 数据库操作类
- * @author lw
- *
- */
- public class DBConn {
- private static Connection conn;
- private static ResultSet rs;
- private static PreparedStatement ps;
- private static String driverClass = "org.logicalcobwebs.proxool.ProxoolDriver";//proxool驱动类
- //private static String url = "jdbc:mysql://localhost:3306/bbs?user=root&password=123";
- private static int activeCount = 0; //活动连接数
- protected final Logger log = Logger.getLogger(DBConn.class.getName());
- /**
- * 获取数据库连接
- * @return Connection
- */
- public Connection getConn() {
- try {
- Class.forName(driverClass);
- conn = DriverManager.getConnection("proxool.smms"); //此处的smms是在proxool.xml中配置的连接池别名
- this.showSnapshotInfo(); //查看连接池信息
- } catch (Exception e) {
- log.error(e.getMessage());
- log.debug("数据库连接错误!");
- //System.out.println(e.getMessage());
- //System.out.println("数据库连接错误!");
- }
- return conn;
- }
- /**
- * 释放连接
- * freeConnection
- * @param conn
- */
- public void freeConnection(Connection conn){
- if(conn!=null){
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 获取连接池中的连接信息
- */
- private void showSnapshotInfo(){
- try{
- SnapshotIF snapshot = ProxoolFacade.getSnapshot("smms", true);
- int curActiveCount=snapshot.getActiveConnectionCount();//获得活动连接数
- int availableCount=snapshot.getAvailableConnectionCount();//获得可得到的连接数
- int maxCount=snapshot.getMaximumConnectionCount() ;//获得总连接数
- if(curActiveCount!=activeCount)//当活动连接数变化时输出的信息
- {
- log.debug("活动连接数:"+curActiveCount+"(active);可得到的连接数:"+availableCount+"(available);总连接数:"+maxCount+"(max)");
- //System.out.println("活动连接数:"+curActiveCount+"(active);可得到的连接数:"+availableCount+"(available);总连接数:"+maxCount+"(max)");
- activeCount=curActiveCount;
- }
- }catch(ProxoolException e){
- e.printStackTrace();
- }
- }
- }
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20