Java – JDO: is persistence manager a singleton?

Just the basics: I use datanucleus to support embedded db4o databases

If I do this simple test:

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

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

I got a file locking exception:

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

This tells me that I don't know how persistence manager should work I think as long as I need a PersistenceManager to query or save data, I will call persistencemanagerfactory, and I will get thread safe things

>Do I need to create the entire singleton application of PersistenceManager? > How to do multiple threads, execute queries and updates, and work in JDO / datanucleus?

Solution

Do I need to make PersistenceManager a singleton throughout the application?

It depends on your application If you are developing desktop applications, you may only need a persistence manager This persistence manager represents the database state of the desktop application But for other cases, this is not the case For example, in a web application, you want to isolate requests or sessions from each other Therefore, you use multiple persistencemanagers For example, one PersistenceManager per request Each persistence manager holds the status and transaction of the current request

Therefore, the PersistenceManager instance represents a unit of work / transaction

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>