Where to declare JDO Singleton PersistenceManagerFactory with multiple services
I am working with GWT and have 4 Service Implementations that need PersistenceManagerFactory. I followed Google's guidelines for creating a singleton class, however I'm not sure where this class should be instantiated and referenced in server-side code.
The class looks like this:
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
public final class PMF {
private static final PersistenceManagerFactory pmfInstance =
JDOHelper.getPersistenceManagerFactory("transactions-optional");
private PMF() {}
public static PersistenceManagerFactory get() {
return pmfInstance;
}
}
But doing something like this in each service implementation seems to result in a unique copy of the singleton based on the error message I get: "Application code tried to create a PersistenceManagerFactory named transaction-optional, but one with that name already exists!"
@Override
public void addCategory(Category category) {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
pm.makePersistent(category);
} finally {
pm.close();
}
}
So basically the first ServiceImpl that calls addCategory is fine, everyone else fails with the error above. I am missing something vital here, I thought the whole point of the singleton class is to create a static PersistenceManagerFactory. Any insight into what I am missing is greatly appreciated.
a source to share