Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping a JDO persistence manager alive instead of closing it?

Does a persistence manager generally need to be closed? Can you just keep one open and re-use it all the time, ie just repeat this pattern:

Transaction tx = pm.currentTransaction();
try {
    tx.begin();
    // do stuff
    tx.commit();
} finally {
    if (tx.isActive()) tx.rollback();
}

What are the downsides of this? It seems to make sense as you would never need to 'detatch' objects due to the persistence manager being closed?

like image 952
Jay Avatar asked Oct 14 '22 02:10

Jay


1 Answers

You can keep it open all the time if you want. The main issue to consider is when you are running 'update' queries, how quickly do you want the changes to take effect. Closing the persistence manager persists these changes immediately, whereas not doing so explicitly will rely upon the datastore to persist your changes at its own convenience. If you are using transactions, this is irrelevant. Aside from that, there's not really any downside. There's a large cpu + time overhead upon the very first initialization of the PM (first use after you deploy), but after that opening/closing the PM is basically free.

like image 171
Travis Webb Avatar answered Oct 16 '22 14:10

Travis Webb