Search code examples
javajdodb4odatanucleus

JDO: Is the PersistenceManager a singleton?


Just the basics: I'm using DataNucleus backed with an embedded DB4O database.

If I do this simple test:

    PersistenceManager pm1 = persistenceManagerFactory.getPersistenceManager();
    PersistenceManager pm2 = persistenceManagerFactory.getPersistenceManager();

    pm1.makePersistent(t1);
    pm2.makePersistent(t2);

I get a file locked exception:

com.db4o.ext.DatabaseFileLockedException: C:\<path>\primary_datastore.data

Which tells me I don't know how the PersistenceManager is supposed to work. I thought I just called PersistenceManagerFactory whenever I needed a PersistenceManager to query or persist data and I would get something thread safe.

  • Do I need to make PersistenceManager a singleton across my entire application?
  • How do multiple threads, performing queries and updates work in JDO/DataNucleus?

Solution

  • Do I need to make PersistenceManager a singleton across my entire application?

    It depends on you application. If you developing a desktop-application you probably only need only one persistence manager. This persistence-manager represent the state of the database for you desktop-app. However for other scenarios this isn't the case. For example in web application you want to isolate the requests or sessions from each other. Therefore you use multiple PersistenceManager. For example a PersistenceManager per request. Each PersistenceManager hold the state and transaction for the current request.

    So a PersistenceManager-instance represents a unit work / transaction.