I have two questions regarding how to create / use the JDO persistence manager (PM, hereafter).
Say, in a Java web application, if I have 10 entities, which can be logically grouped into 2 groups (for example, 5 user related entities and 5 business related entities)
PersistenceManager is the primary interface for JDO-aware application components. It is the factory for Query and Transaction instances, and contains methods to manage the life cycle of PersistenceCapable instances. A PersistenceManager is obtained from the PersistenceManagerFactory (recommended) or by construction.
JPA persists java objects to relational data via ORM, while JDO is more general specification for java object persistence. So using JDO will give you more freedom in storage implementation options for your objects.
According to the JDO Documentation you create one PersistenceManagerFactory
per datastore. If you are using JDO to access databases via SQL and you have more than one database, then you will need one PersistenceManagerFactory
per database (since you need to specify the JDBC URL, user name and password when you create the PersistenceManagerFactory
).
For simple use cases, you can just create a PersistenceManager
when you need it and close it in a finally
clause (see the persistence manager documentation).
If you use transactions, and the code for updating entities can be spread across multiple methods or objects, I recommend creating the PersistenceManager
on demand and storing it in a ThreadLocal
(or request-scoped object if you use Guice or Spring). This will make sure any code that does updates participates in the current transaction. Make sure to close the PersistenceManager
at the end of the request.
If you only need one persistence manager factory, you can do:
public class Datastore {
private static PersistenceManagerFactory PMF;
private static final ThreadLocal<PersistenceManager> PER_THREAD_PM
= new ThreadLocal<PersistenceManager>();
public static void initialize() {
if (PMF != null) {
throw new IllegalStateException("initialize() already called");
}
PMF = JDOHelper.getPersistenceManagerFactory("jdo.properties");
}
public static PersistenceManager getPersistenceManager() {
PersistenceManager pm = PER_THREAD_PM.get();
if (pm == null) {
pm = PMF.getPersistenceManager();
PER_THREAD_PM.set(pm);
}
return pm;
}
public static void finishRequest() {
PersistenceManager pm = PER_THREAD_PM.get();
if (pm != null) {
PER_THREAD_PM.remove();
Transaction tx = pm.currentTransaction();
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
}
Any code that needs a persistence manager can call Datastore.getPersistenceManager()
Note: I used all static methods to make it simple for the purposes of answering your question. If I was using a dependency-injection framework like Guice, I would make the methods non-static and bind Datastore
as a Singleton.
You could call finishRequest
in a Servlet Filter:
public class PersistenceManagerFilter implements javax.servlet.Filter {
public init(FilterConfig filterConfig) {
Datastore.initialize();
}
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain)
throws IOException, ServletException {
try {
chain.doFilter(request, response);
} finally {
Datastore.finishRequest();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With