[OJB-developers] Single VM Object Cache Consistency?
Brought to you by:
thma
From: John F. <jfr...@so...> - 2002-04-11 19:57:15
|
I've worked through just about every technical question I have on OJB's applicability to my current project. The last nagging question I have is about object cache consistency. I see that within a single VM that multiple PersistenceBrokers share a singleton object cache. Isn't this a problem for multi-threaded applications (like a servlet container)? For example: Thread A gets a PB from the pool Thread B gets a PB from the pool Thread A calls PB.beginTransaction() Thread B calls PB.beginTransaction() Thread A looks up the xyz BankAccount object - this ends up loading the BankAccount object into the shared object cache Thread B looks up the xyz BankAccount object and is returned a reference to the same object instance from the shared object cache Thread A & B concurrently access the single xyz BankAccount object, possibly corrupting things, but each is certainly unaware that the other thread even exists Thread A calls PB.store(xyz BankAccount object) Thread B calls PB.store(xyz BankAccount object) Thread A calls PB.commitTransaction() Thread B calls PB.commitTransaction() In this scenario, the db transactions within each thread become intertwined in a way because each thread has no idea the other thread may have made additional changes to the object state within the context of its own transaction. It could easily end up committing unintended state changes. Also, this would seem to indicate that every class that could be persisted with OJB would need to be either immutable or thread safe (if you are using OJB in this sort of context). Is there some other common way folks get around this that I'm completely missing (or something else OJB is doing behind the scenes)? It really seems like in this sort of context that each PB should have its own object cache which is flushed after each transaction commit/rollback. Or create a more sophisticated object cache that you could mark some classes as immutable so that they could be shared across transactions & threads this way. Actually, it looks like the MetaObjectCacheImpl may be just what I'm looking for. Thoughts? Thanks much, - John Freeborg |