参考:http://proxool.sourceforge.net/user.html
连接数据库
1. 按照传统方式连接数据库
Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe");
2. 用Proxool连接数据库
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); Connection conn = DriverManager.getConnection("proxool.mypool:oracle.jdbc.driver.OracleDriver:jdbc:oracle:thin:@localhost:1521:xe");
配置Proxool
1. ①用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>mypool</alias> <driver-url>jdbc:oracle:thin:@localhost:1521:xe</driver-url> <driver-class>oracle.jdbc.OracleDriver</driver-class> <driver-properties> <property name="user" value="myname"/> <property name="password" value="mypassword"/> </driver-properties> <maximum-connection-count>10</maximum-connection-count> <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
<statistics>1m,15m,1d</statistics> </proxool> </something-else-entirely>
1. ②用JAXPConfigurator解析配置文件,需要Crimson或者Xerces
JAXPConfigurator.configure("src/proxool-cfg.xml", false); // The false means non-validating
1. ③使用
Connection conn = DriverManager.getConnection("proxool.mypool");
2. ①用properties文件
jdbc-0.proxool.alias = mypool jdbc-0.proxool.driver-url = jdbc:oracle:thin:@localhost:1521:xe jdbc-0.proxool.driver-class = oracle.jdbc.OracleDriver jdbc-0.user = myname jdbc-0.password = mypassword jdbc-0.proxool.maximum-connection-count = 10 jdbc-0.proxool.house-keeping-test-sql = select CURRENT_DATE
jdbc-0.proxool.statistics = 1m,15m,1d
2. ②用PropertyConfigurator解析配置文件
PropertyConfigurator.configure("src/proxool-cfg.properties");
2. ③使用
Connection conn = DriverManager.getConnection("proxool.mypool");
各属性的含义:http://proxool.sourceforge.net/properties.html
配置AdminServlet:查看统计数据
<servlet> <servlet-name>proxool_admin</servlet-name> <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>proxool_admin</servlet-name> <url-pattern>/admin</url-pattern> </servlet-mapping>
在Servlet环境下配置Proxool
1. 用已有的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>
2. 用已有的properties文件
<servlet> <servlet-name>ServletConfigurator</servlet-name> <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>
<init-param>
<param-name>propertyFile</param-name>
<param-value>WEB-INF/proxool.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup> </servlet>
3. 用Servlet的初始化参数配置
<servlet> <servlet-name>ServletConfigurator</servlet-name> <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class> <init-param>
<param-name>jdbc-0.proxool.alias</param-name>
<param-value>mypool</param-value>
</init-param>
<init-param>
<param-name>jdbc-0.proxool.driver-url</param-name>
<param-value>jdbc:oracle:thin:@localhost:1521:xe</param-value>
</init-param>
<init-param>
<param-name>jdbc-0.proxool.driver-class</param-name>
<param-value>oracle.jdbc.OracleDriver</param-value>
</init-param>
<load-on-startup>1</load-on-startup> </servlet>
4. 不让Proxool自动关闭
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>
<init-param>
<param-name>autoShutdown</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup> </servlet>
JAR的位置
1. 放在WEB-INF/lib下,
①不能被多个Web项目共享
②重新加载Web项目的时候,会新建一个Proxool实例,而原来的实例还在继续运行(占用资源),可以用ServletConfigurator关闭或者调用shutdown方法
2. 放在Servlet容器的公共lib下或者.classpath下,
①可以让很多个Web项目共享
②重新加载Web项目的时候,并不会新建实例,原来的实例还在继续运行,重启连接池的方法是重启Servlet容器或者shutdown再重新配置
Proxool的工作方式
Proxool就像一个代理,这样的好处就是通过配置,可以在不同的JDBC中切换,而数据库连接的生命周期不会有变化。
当然你调用conn.close()时,这个连接并没有关闭而是放回了连接池中。