
JPOX is developed as a plugin-driven framework and one of the components that is pluggable is the pooling
of connections to the datastore. JPOX provides a large selection of
connection pools (DBCP, C3P0, Proxool) but is structured so that you can easily add your own variant and have it
usable within your JPOX usage. This is available in JPOX from version 1.1.2
The following sections describe how to create your own connection pooling plugin for JPOX.
If you have your own DataSource connection pooling implementation you can easily use it with JPOX.
JPOX defines a DataSourceFactory interface and you need to implement this.
package org.jpox.datasource;
public interface JPOXDataSourceFactory
{
/** Localisation of messages. */
public static final Localiser LOCALISER = Localiser.getInstance("org.jpox.Localisation");
/**
* Method to make a DataSource for use within JPOX.
* @param dbDriver The database driver name
* @param dbURL The database URL
* @param dbUser The database username
* @param dbPassword The database password
* @param configFile Location of any configuration properties file
* @param clr ClassLoaderResolver to use
* @return The DataSource
* @throws Exception Thrown if an error occurs during creation
*/
public DataSource makePooledDataSource(String dbDriver,
String dbURL,
String dbUser,
String dbPassword,
String configFile,
ClassLoaderResolver clr);
}
So let's suppose you have a library (org.mydomain.MyPoolingClass) that creates a DataSource that
handles pooling. So you would do something like this
package mydomain;
import org.jpox.ClassLoaderResolver;
import org.jpox.datasource.JPOXDataSourceFactory;
public class MyPoolingClassDataSourceFactory implements JPOXDataSourceFactory
{
/**
* Method to make a MyPoolingClass DataSource for use internally in JPOX.
* @param dbDriver The database driver name
* @param dbURL The database URL
* @param dbUser The database username
* @param dbPassword The database password
* @param configFile Location of any configuration properties file
* @param clr ClassLoaderResolver to use
* @return The DataSource
* @throws Exception Thrown if an error occurs during creation
*/
public DataSource makePooledDataSource(String dbDriver,
String dbURL,
String dbUser,
String dbPassword,
String configFile,
ClassLoaderResolver clr)
{
// Load the database driver
try
{
Class.forName(dbDriver);
}
catch (ClassNotFoundException cnfe)
{
try
{
clr.classForName(dbDriver);
}
catch (RuntimeException e)
{
// JDBC driver not found
throw new DatastoreDriverNotFoundException(dbDriver);
}
}
// Check the presence of "mydomain.MyPoolingClass"
try
{
Class.forName("mydomain.MyPoolingClass");
}
catch (ClassNotFoundException cnfe)
{
try
{
clr.classForName("mydomain.MyPoolingClass");
}
catch (RuntimeException e)
{
// "MyPoolingClass" library not found
throw new DatastoreLibraryNotFoundException("MyPoolingClass", "MyPoolingClass");
}
}
// Create the Data Source for your pooling library
// Use the input driver, URL, user/password
// Use the input configuration file as required
DataSource ds = new mydomain.MyPoolingClass(...);
return ds;
}
}
The only thing required now is to register this plugin with JPOX when you start up your application.
To do this create a file plugin.xml and put it in your JAR either at the root of the CLASSPATH or
in "META-INF/plugins". It should look like this
<?xml version="1.0"?>
<plugin id="mydomain" name="JPOX plug-ins" provider-name="My Company">
<extension point="org.jpox.datasource">
<datasource-factory name="MyPoolingClass" class-name="mydomain.MyPoolingClassDataSourceFactory"/>
</extension>
</plugin>
The only thing remaining is to use your new JPOXDataSourceFactory plugin. You do this by having your plugin
in the CLASSPATH at runtime, and setting the PMF property org.jpox.connectionPoolingType to MyPoolingClass
(the name you specified in the plugin.xml file).