My application needs to take action whenever optimistic concurrency control fails (for example, whenever a database change fails because the version of an affected row is different from the version last read). As I understand it, whenever Hibernate detects this it throws a StaleObjectStateException, which is helpful. But it's a nuisance that StaleObjectStateException might propagate from any one of many Hibernate methods.
I'd like one method in my application to be called whenever Hibernate throws a StaleObjectStateException. Can you recommend a way to implement that? I'd be willing to make Hibernate 'observable' (in the Design Patterns sense), and interested in the Hibernate developers' opinion of that idea.
Currently I'm wrapping a java.lang.reflect.Proxy around every Hibernate Session, with an InvocationHandler that catches StaleObjectStateException. And I also have a similar exception handler where I call UserTransaction.commit. But I worry that these aren't sufficient, or that I can't afford the performance penalty of a Proxy.
Theoretically all my exception handlers could catch StaleObjectStateException and respond as desired. But in practice there are many such exception handlers, so that would be too much code to write and maintain.
P.S. In case you're curious, I intend to respond to StaleObjectStateException by evicting stale entries from the global cache. My application can obtain a summary of recent database changes and use it to identify stale cache entries, but the algorithm for doing so has a fairly high fixed cost, so I don't want to do it frequently unless optimistic concurrency control fails.
|