Any JDO-enabled application will require at least one PersistenceManagerFactory . Typically applications create one per datastore being utilised. A PersistenceManagerFactory provides access to PersistenceManager s which allow objects to be persisted, and retrieved. The PersistenceManagerFactory can be configured to provide particular behaviour.
The simplest way of creating a
PersistenceManagerFactory
Properties properties = new Properties();
properties.setProperty("javax.jdo.PersistenceManagerFactoryClass",
"org.datanucleus.api.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");
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);A slight variation on this, is to use a file to specify these properties like this javax.jdo.PersistenceManagerFactoryClass=org.datanucleus.api.jdo.JDOPersistenceManagerFactory javax.jdo.option.ConnectionDriverName=com.mysql.jdbc.Driver javax.jdo.option.ConnectionURL=jdbc:mysql://localhost/myDB javax.jdo.option.ConnectionUserName=login javax.jdo.option.ConnectionPassword=password and then to create the PersistenceManagerFactory using this file File propsFile = new File(filename); PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(propsFile); Another alternative would be to call JDOHelper.getPersistenceManagerFactory(jndiLocation, context); , hence accessing the properties via JNDI. A PersistenceManagerFactory is designed to be thread-safe. A PersistenceManager is not Whichever way we wish to obtain the PersistenceManagerFactory we have defined a series of properties to give the behaviour of the PersistenceManagerFactory . The first property specifies to use the DataNucleus implementation, and the following 4 properties define the datastore that it should connect to. There are many properties available. Some of these are standard JDO properties, and some are DataNucleus extensions.
DataNucleus provides many properties to extend the control that JDO gives you. These can be used alongside the above standard JDO properties, but will only work with DataNucleus. Please consult the Persistence Properties Guide for full details. |