One transaction or two?

#1

If I have a large number of objects to delete and then add to a PersistenceManager is it more efficient to do everything in one transaction or two? Currently my code looks like this:

PersistenceManager pm = m_pmFactory.getPersistenceManager();
try {
    pm.currentTransaction().begin();
    pm.deletePersistentAll(toDelete);
    pm.makePersistentAll(toAdd);
    pm.currentTransaction().end();
} finally {
    if(pm.currentTransaction().isActive()) {
        pm.currentTransaction().rollback();
    }
    if(!pm.isClosed()) {
        pm.close();
    }
}

Should I separate the deletes and adds into separate transactions? I think I remember reading in another forum post that you recommended this, I just wanted to clarify.

#2

Actually one transaction eliminates some overhead but may require more RAM if you have many large objects, and that may also affect performance.

I don't expect a large difference but you may want to try both options and see which is faster in your specific case.

ObjectDB Support
#3

Yeah I ran a test overnight and didn't notice any appreciable difference in performance so I think I'm just going to leave it as a single transaction. Thanks for the input!

#4

Another question based on the same code bit above. Whenever I have objects to add/remove from the database they are submitted to an ExecutorService task. Each task then gets a Persistence Manager from the factory, performs its transaction and then closes the Persistence Manager. Under heavy load this can happen two to three hundred times per minute often times with several threads executing in parallel each with their own Persistence Manager. So over the life of the application many thousands of these are performed. Would there be much benefit to maintain a pool of Persistence Manager instances to be used across these tasks? I would include some synchronization logic to ensure that each thread had its own Persistence Manager. Is the act of creating/closing Persistence Managers heavy enough that this would provide much of a benefit?

#5

The overhead of creating an EntityManager / PersistenceManager is low. ObjectDB already manages a pool of underlying resources (e.g. connection to the database / open database file) to keep the instantiation of these objects inexpensive.

But ObjectDB doesn't use a pool of EntityManager / PersistenceManager instances (just of some underlying resources are pooled) so if you implement such a pool you may gain some performance in some cases (e.g. if the L1 cache is important in this application). On the hand such a solution is expected to consume more memory.

Since you are asking about add/remove operations rather than read operation (in which the L1 cache is important) - performance gain, if any, is expected to be minimal. But again, to get an accurate answer you will have to try it.

ObjectDB Support

Reply