
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.
JPOX requires a DataSource to define the datastore in use and consequently allows use of connection pooling.
JPOX provides 3 plugins for different pooling products - DBCP, C3P0, and Proxool. You can easily define your
own plugin for pooling. You can extend JPOX's capabilities using the plugin extension org.jpox.datasource.
| Plugin extension-point | Key | Description | Location |
|---|
| org.jpox.datasource | c3p0 | RDBMS connection pool, using C3P0 | jpox-c3p0 |
| org.jpox.datasource | dbcp | RDBMS connection pool, using Apache DBCP | jpox-dbcp |
| org.jpox.datasource | proxool | RDBMS connection pool, using Proxool | jpox-proxool |
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.store.rdbms.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 omfCtx OMF Context
* @return The DataSource
* @throws Exception Thrown if an error occurs during creation
*/
public DataSource makePooledDataSource(OMFContext omfCtx);
}
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.store.rdbms.datasource.JPOXDataSourceFactory;
public class MyPoolingClassDataSourceFactory implements JPOXDataSourceFactory
{
/**
* Method to make a DataSource for use within JPOX.
* @param omfCtx OMF Context
* @return The DataSource
* @throws Exception Thrown if an error occurs during creation
*/
public DataSource makePooledDataSource(OMFContext omfCtx)
{
PersistenceConfiguration conf = omfCtx.getPersistenceConfiguration();
String dbDriver = conf.getStringProperty("org.jpox.ConnectionDriverName");
String dbURL = conf.getStringProperty("org.jpox.ConnectionURL");
String dbUser = conf.getStringProperty("org.jpox.ConnectionUserName");
String dbPassword = conf.getStringProperty("org.jpox.ConnectionPassword");
ClassLoaderResolver clr = omfCtx.getClassLoaderResolver(null);
// 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 at the root of the CLASSPATH.
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 persistence property
org.jpox.connectionPoolingType to MyPoolingClass
(the name you specified in the plugin.xml file).