1.先下载Proxool连接池
地址:http://sourceforge.net/projects/proxool/files/proxool/0.9.1/proxool-0.9.1.zip/download
2.将proxool包导入到项目中,proxool-0.9.1.jar、proxool-cglib.jar,(注意:这里还要导入commons-logging.jar包,proxool没有自带这个包)
3、在WEB-INF下建proxool.xml文件,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <something-else-entirely> <proxool> <alias>OA</alias> <driver-url>jdbc:oracle:thin:@127.0.0.1:1521:orcl</driver-url> <driver-class>oracle.jdbc.driver.OracleDriver</driver-class> <driver-properties> <property name="user" value="HCOA"></property> <property name="password" value="HCOA"></property> </driver-properties> <!-- 最大连接数(默认15个),超过这个连接数,再有请求时,就排在队列中等候,最大的等待请求数maxnmun-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 sysdate from dual</house-keeping-test-sql> </proxool> </something-else-entirely>
这里请注意 如果你拷贝我的代码出现的错误 请手动敲进去 这里可能是编码的问题 或者将中文去掉
4.修改WEB.XML,代码如下:
<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>
5.写方法调用,我这里贴出代码,如有错误请指出,谢谢!
package com.fp.comm; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import org.logicalcobwebs.proxool.ProxoolException; import org.logicalcobwebs.proxool.ProxoolFacade; import org.logicalcobwebs.proxool.admin.SnapshotIF; public class PoolManager { private static int activeCount = 0; //单例 唯一实例 private static class PoolManagerInstanse{ private static final PoolManager instance = new PoolManager(); } public static PoolManager getInstance(){ return PoolManagerInstanse.instance; } public Connection getConnection(){ Connection conn = null; try{ Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); //加载驱动 conn = DriverManager.getConnection("proxool.OA"); //此处的DBPool是在proxool.xml中配置的连接池别名 showSnapshotInfo(); }catch(Exception e){ System.out.println(e.getMessage()); } return conn; } /** * 此方法可以得到连接池的信息 * showSnapshotInfo */ private void showSnapshotInfo(){ try{ SnapshotIF snapshot = ProxoolFacade.getSnapshot("OA", 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(); } } /** * 释放连接 * freeConnection * @param conn */ public static void freeConnection(Connection conn){ if(conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) { PoolManager pool1 = PoolManager.getInstance(); PoolManager pool2 = PoolManager.getInstance(); if(pool1 == pool2){ System.out.println(true); }else{ System.out.println(false); } } }
6。获取连接的方法
Connection conn = PoolManager.getInstance().getConnection();