Written by coregps on Wednesday, May 18th, 2005 in Java.
What is Proxool?
Proxool is an open source Java connection pool, which can add connection pooling to your existing JDBC driver transparently. It is very easy to configure, and you can migrate your code in a walk.
Download
For the most up-to-date version, go to the Proxool download site. As of this date, the current version is Proxool 0.8.3. Download the Proxool zip file to your local machine.
Install
Unzip the proxool-0.8.3.zip file to a directory, You will find proxool-0.8.3.jar in the lib directory. Copy it to your web application’s WEB-INF/lib directory.
Configuration
For ease of changing the database parameters, I will configure the pools by using an XML file or a properties file.
First, I will explain the solution using an XML file. This can be achieved by the following steps:
1. Create an XML file named proxool.xml (you can name it whatever you like), like following:
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<proxool>
<alias>myPool</alias>
<driver-url>jdbc:mysql://localhost:3306/demodb</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name=”user” value=”root”/>
<property name=”password” value=”secret”/>
</driver-properties>
<test-before-use>true</test-before-use>
<maximum-connection-lifetime>60000</maximum-connection-lifetime>
<prototype-count>5</prototype-count>
<maximum-connection-count>50</maximum-connection-count>
<house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
</proxool>
2. Configure the pools at startup by adding the following lines in 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>
</servlet>
Note that when using this solution, you must download a JAXP compliant XML parser. Here I will use Xerces, download the zip file, unzip it and put all jar files into your web application’s WEB-INF/lib directory.
Another solution I will explain is using a properties file. This can be done also in two steps:
1. Create a propreties file named proxool.properties (you can name it whatever you like), like following:
jdbc-0.proxool.alias=myPool
jdbc-0.proxool.driver-class=com.mysql.jdbc.Driver
jdbc-0.proxool.driver-url=jdbc:mysql://localhost:3306/demodb
jdbc-0.user=root
jdbc-0.password=secret
jdbc-0.proxool.maximum-connection-count=40
jdbc-0.proxool.prototype-count=4
jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE
jdbc-0.proxool.verbose=true
jdbc-0.proxool.statistics=10s,1m,1d
jdbc-0.proxool.statistics-log-level=DEBUG
2. Configure the pools at startup by adding the following lines in web.xml:
<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>
Using
Once you’ve done the steps above, restart Tomcat. And now we can pick up a connection by using the alias configured above. The following is a Java Bean used to retrieve a connection from the connection pool.
package com.esurfer.common;
import java.sql.*;
public class DBConn {
public static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(”proxool.myPool”);
} catch (Exception e) {
System.out.println(”Get Connection Failed!” + e);
}
return conn;
}
}
We can use the connection object retrieved to create statements, like following:
Connection conn = DBConn.getConnection();
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
Lesson
Be sure not to lost the the following line when you configure your servlet.
<load-on-startup>1</load-on-startup>
If you lost it, you will get error message something like java.sql.SQLException: No suitable driver. It seems that Proxool itself can’t find the driver.
For the full list of all the properties, visit http://proxool.sourceforge.net/properties.html.
For more information about Proxool, visit http://proxool.sourceforge.net/index.html.