码迷,mamicode.com
首页 > 其他好文 > 详细

Proxool

时间:2015-12-22 23:06:03      阅读:500      评论:0      收藏:0      [点我收藏+]

标签:

参考: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>org.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 = org.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</servlet-name>
  <servlet-class>org.logicalcobwebs.proxool.admin.AdminServlet</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>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>org.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()时,这个连接并没有关闭而是放回了连接池中。

Proxool

标签:

原文地址:http://www.cnblogs.com/hippiebaby/p/5068286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!