views:

117

answers:

1

Hello, I am working with GWT and have 4 Service Implementations that need a PersistenceManagerFactory. I followed Google's advice on creating a singleton class, however I am unsure of where this class should be instantiated and referenced from in the 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 end up creating a unique copy of the singleton based on the error message I get "Application code attempted to create a PersistenceManagerFactory named transactions-optional, but one with this 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, all others fail 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 insights into what I am missing are greatly appreciated.

A: 

The PMF class should be a top level class, not an internal class.

Usually its site.server.PMF.

antony.trupe
@Zach I'm curious. Was that what your problem was, or did that just help you figure something else out?
antony.trupe
In general, I was confused about how singletons are used, how they become instantiated etc. Your post helped me make sense of the singleton ecosystem.
Error 454
@Error wonderful, glad to have helped someone.
antony.trupe