Named PersistenceManagerFactory

Typically applications create one PMF per datastore being utilised. JDO2.1+ provides a way of creating a PMF with a particular name and set of properties. This utilises a configuration file that defines the named PMFs. This configuration file is called jdoconfig.xml , and is located under "META-INF/". Let's see an example of a jdoconfig.xml

<?xml version="1.0" encoding="utf-8"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">

    <!-- Datastore Txn PMF -->
    <persistence-manager-factory name="Datastore">
        <property name="javax.jdo.PersistenceManagerFactoryClass"
            value="org.datanucleus.jdo.JDOPersistenceManagerFactory"/>
        <property name="javax.jdo.option.ConnectionDriverName"
            value="com.mysql.jdbc.Driver"/>
        <property name="javax.jdo.option.ConnectionURL"
            value="jdbc:mysql://localhost/datanucleus?useServerPrepStmts=false"/>
        <property name="javax.jdo.option.ConnectionUserName"
            value="datanucleus"/>
        <property name="javax.jdo.option.ConnectionPassword"
            value=""/>
        <property name="javax.jdo.option.Optimistic"
            value="false"/>
        <property name="datanucleus.autoCreateSchema"
            value="true"/>
    </persistence-manager-factory>

    <!-- Optimistic Txn PMF -->
    <persistence-manager-factory name="Optimistic">
        <property name="javax.jdo.PersistenceManagerFactoryClass"
            value="org.datanucleus.jdo.JDOPersistenceManagerFactory"/>
        <property name="javax.jdo.option.ConnectionDriverName"
            value="com.mysql.jdbc.Driver"/>
        <property name="javax.jdo.option.ConnectionURL"
            value="jdbc:mysql://localhost/datanucleus?useServerPrepStmts=false"/>
        <property name="javax.jdo.option.ConnectionUserName"
            value="datanucleus"/>
        <property name="javax.jdo.option.ConnectionPassword"
            value=""/>
        <property name="javax.jdo.option.Optimistic"
            value="true"/>
        <property name="datanucleus.autoCreateSchema"
            value="true"/>
    </persistence-manager-factory>

</jdoconfig>

So in this example we have 2 named PMFs. The first is known by the name "Datastore" and utilises datastore transactions. The second is known by the name "Optimistic" and utilises optimistic transactions. You simply define all properties for the particular PMF within its specification block. And finally we instantiate our PMF like this

PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("Optimistic");

That's it. The PMF we are returned from JDOHelper will have all of the properties defined in jdoconfig.xml under the name of "Optimistic".