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
JPOX Runtime
RDBMS Datastores
DB4O Datastore
RDBMS Connection Pools

When you create a PersistenceManagerFactory you define the connection URL, driver name, and the username/password to use. This works perfectly well but does not "pool" the connections so that they are efficiently opened/closed when needed to utilise datastore resources in an optimum way. JPOX allows you to utilise a connection pool to efficiently manage the connections to the datastore. You can do this using one of the following ways currently.



JPOX plugin : DBCP

JPOX has a plugin available for DBCP. This is accessed by specifying the PersistenceManagerFactory property org.jpox.connectionPoolingType. To utilise DBCP-based connection pooling we do this

// Create our PMF for using JPOX
Properties props = new Properties();
properties.setProperty("javax.jdo.PersistenceManagerFactoryClass","org.jpox.jdo.JDOPersistenceManagerFactory");
properties.setProperty("javax.jdo.option.ConnectionDriverName","com.mysql.jdbc.Driver");
properties.setProperty("javax.jdo.option.ConnectionURL","jdbc:mysql://localhost/myDB");
properties.setProperty("javax.jdo.option.ConnectionUserName","login");
properties.setProperty("javax.jdo.option.ConnectionPassword","password");
properties.setProperty("org.jpox.connectionPoolingType", "DBCP");
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);
                

So this PersistenceManagerFactory will use connection pooling using DBCP. To do this you will need the JPOX DBCP plugin to be in the CLASSPATH, along with the commons-dbcp, commons-pool and commons-collections JARs.

You can also specify persistence properties to control the actual pooling. The currently supported properties for DBCP are shown below

# Pooling of Connections
org.jpox.connectionPool.maxIdle=10
org.jpox.connectionPool.minIdle=3
org.jpox.connectionPool.maxActive=5
org.jpox.connectionPool.maxWait=60

# Pooling of PreparedStatements
org.jpox.connectionPool.maxStatements=20

org.jpox.connectionPool.timeBetweenEvictionRunsMillis=2400000
org.jpox.connectionPool.minEvictableIdleTimeMillis=18000000


JPOX plugin : C3P0

JPOX has a plugin available for C3P0. This is accessed by specifying the PersistenceManagerFactory property org.jpox.connectionPoolingType. To utilise C3P0-based connection pooling we do this

// Create our PMF for using JPOX
Properties props = new Properties();
properties.setProperty("javax.jdo.PersistenceManagerFactoryClass","org.jpox.jdo.JDOPersistenceManagerFactory");
properties.setProperty("javax.jdo.option.ConnectionDriverName","com.mysql.jdbc.Driver");
properties.setProperty("javax.jdo.option.ConnectionURL","jdbc:mysql://localhost/myDB");
properties.setProperty("javax.jdo.option.ConnectionUserName","login");
properties.setProperty("javax.jdo.option.ConnectionPassword","password");
properties.setProperty("org.jpox.connectionPoolingType", "C3P0");
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);
                

So this PersistenceManagerFactory will use connection pooling using C3P0. To do this you will need the JPOX C3P0 plugin in your CLASSPATH together with the C3P0 JAR. If you want to configure C3P0 further you can include a "c3p0.properties" in your CLASSPATH - see the C3P0 documentation for details.

You can also specify persistence properties to control the actual pooling. The currently supported properties for C3P0 are shown below

# Pooling of Connections
org.jpox.connectionPool.maxPoolSize=5
org.jpox.connectionPool.minPoolSize=3
org.jpox.connectionPool.initialPoolSize=3

# Pooling of PreparedStatements
org.jpox.connectionPool.maxStatements=20


JPOX plugin : Proxool

JPOX has a plugin available for Proxool. This is accessed by specifying the PersistenceManagerFactory property org.jpox.connectionPoolingType. To utilise Proxool-based connection pooling we do this

// Create our PMF for using JPOX
Properties props = new Properties();
properties.setProperty("javax.jdo.PersistenceManagerFactoryClass","org.jpox.jdo.JDOPersistenceManagerFactory");
properties.setProperty("javax.jdo.option.ConnectionDriverName","com.mysql.jdbc.Driver");
properties.setProperty("javax.jdo.option.ConnectionURL","jdbc:mysql://localhost/myDB");
properties.setProperty("javax.jdo.option.ConnectionUserName","login");
properties.setProperty("javax.jdo.option.ConnectionPassword","password");
properties.setProperty("org.jpox.connectionPoolingType", "Proxool");
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);
                

So this PersistenceManagerFactory will use connection pooling using Proxool To do this you will need the JPOX Proxool plugin to be in the CLASSPATH, along with the proxool and commons-logging JARs.

You can also specify persistence properties to control the actual pooling. The currently supported properties for Proxool are shown below

org.jpox.connectionPool.maxConnections=10
org.jpox.connectionPool.testSQL=SELECT 1


Lookup a DataSource using JNDI

JPOX allows you to use connection pools (java.sql.DataSource) bound to a javax.naming.InitialContext with a JNDI name. You first need to create the DataSource in the container (application server/web server), and secondly you define the javax.jdo.option.ConnectionFactoryName property with the DataSource JDNI name.

The following example uses a properties file that is loaded before creating the PersistenceManagerFactory. The PersistenceManagerFactory is created using the JDOHelper.

javax.jdo.PersistenceManagerFactoryClass=org.jpox.jdo.JDOPersistenceManagerFactory
javax.jdo.option.ConnectionFactoryName=YOUR_DATASOURCE_JNDI_NAME
Properties properties = new Properties();

// the properties file is in your classpath
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("/yourpath/yourfile.properties");

Please read more about this in the Data Source Guide.



Manually create a DataSource ConnectionFactory

We could have used the JPOX plugin which internally creates a DataSource ConnectionFactory, however we can also do this manually if we so wish. Let's demonstrate how to do this with one of the most used pools Jakarta DBCP

With DBCP you need to generate a javax.sql.DataSource, which you will then pass to JPOX. You do this as follows

// Load the JDBC driver
Class.forName(dbDriver);

// Create the actual pool of connections 
ObjectPool connectionPool = new GenericObjectPool(null);

// Create the factory to be used by the pool to create the connections
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dbURL, dbUser, dbPassword);

// Create a factory for caching the PreparedStatements
KeyedObjectPoolFactory kpf = new StackKeyedObjectPoolFactory(null, 20);

// Wrap the connections with pooled variants
PoolableConnectionFactory pcf = new PoolableConnectionFactory(connectionFactory, connectionPool, kpf, 
                                                              null, false, true);

// Create the datasource
DataSource ds = new PoolingDataSource(connectionPool);

// Create our PMF for using JPOX
PersistenceManagerFactory pmf = new org.jpox.jdo.JDOPersistenceManagerFactory();
pmf.setConnectionDriverName(dbDriver);
pmf.setConnectionURL(dbURL);
pmf.setConnectionFactory(ds);

// Set any other properties on the PMF.
                

As you see use the PMF constructor to create our factory. The reason is that the JDOHelper alternative doesn't allow us to specify the data source to be used. Also we haven't passed the dbUser and dbPassword to the PMF since we no longer need to specify them - they are defined for the pool so we let it do the work. As you also see, we use the pmf.setConnectionFactory() method to assign the data source to the PMF. Thereafter we can sit back and enjoy the performance benefits. Please refer to the documentation for DBCP for details of its configurability (you will need commons-dbcp, commons-pool, and commons-collections in your CLASSPATH to use this above example).