JPOX
JPOX
 Project  |  Ver 1.1  |  Ver 1.2  |  JDO  |  JPA  |  Guides  |  Tools
1.2 | Persistence | JDO ORM | JPA ORM | Runtime | JDO Runtime | JPA Runtime | Extensions | Developer
Extensions
Plugins : Connection Pooling

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-pointKeyDescriptionLocation
org.jpox.datasourcec3p0RDBMS connection pool, using C3P0jpox-c3p0
org.jpox.datasourcedbcpRDBMS connection pool, using Apache DBCPjpox-dbcp
org.jpox.datasourceproxoolRDBMS connection pool, using Proxooljpox-proxool

The following sections describe how to create your own connection pooling plugin for JPOX.

Interface

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);
}


Implementation

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;
    }
}


Plugin Specification

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>
Plugin Usage

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).