When you create a
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.
DataNucleus allows you to utilise a connection pool to efficiently manage the connections to the
datastore. We currently provide support for the following
You need to specify the persistence property
to be whichever
of the external pooling libraries you wish to use (or "None" if you explicitly want no pooling). If you
are using JDK1.6+ and want to use the builtin DBCP, you can set this to "dbcp-builtin" (or leave it
to work it out).
DataNucleus provides two sets of connections to the datastore - one for transactional usage, and one
for non-transactional usage. If you want to define a different pooling for nontransactional usage then
you can also specify the persistence property
whichever is required.
DataNucleus allows you to use connection pools (java.sql.DataSource) bound to a
with a JNDI name. You first need to
create the DataSource in the container (application server/web server), and
secondly you define the
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.
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.
We could have used the DataNucleus 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
With DBCP you need to generate a
, which you will then
pass to DataNucleus. You do this as follows
// Load the JDBC driver
// 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
Map properties = new HashMap();
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);
Note that we haven't passed the
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 set the data source for 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
in your CLASSPATH to use this above example).