Java Reference
In-Depth Information
locking failure occurs. Hibernate throws a StaleObjectStateException and JDO
throws a JDOOptimisticVerificationException , which are both mapped by Spring
to a subclass of ObjectOptimisticLockingFailureException . The presentation tier,
which catches the exception, does not immediately know whether it was caused by
an optimistic offline locking failure or a regular optimistic offline locking failure.
One solution is to force Hibernate or JDO to verify that the object is
unchanged in the database immediately after attaching it. A Hibernate applica-
tion can do this by calling Session.lock() , and a JDO application can call Persis-
tenceManager.flush() or PersistenceManager.checkConsistency() . If the object
has changed in the database since it was detached, the persistence framework will
throw an exception. Provided that the application has not updated any other
objects, it can assume that the exception is caused by an optimistic offline locking
failure. The downside of this approach is that prematurely flushing changes can
reduce performance because the persistence framework has less opportunity to
optimize database accesses.
The other way to determine whether the exception was caused by an offline
locking error is to examine the ObjectOptimisticLockingFailureException ,
which contains the class and ID of the object that failed the optimistic locking.
This solution avoids premature and potentially inefficient flushing of changes to
the database, but does have the drawback of making the exception-handling logic
more complicated.
When to use this approach
An application should use this approach when:
It simplifies how the presentation tier passes the user's changes to the busi-
ness tier.
The persistence framework does not provide access to the version number
or timestamp.
Optimistic locking is implemented using state comparison instead version
numbers or timestamps.
It edits a graph of objects rather than an individual object.
Detached objects are a very convenient way to implement the Optimistic Offline
Lock pattern. Let's take an in-depth look at an implementation of the Acknowledge-
OrderService that uses them.
 
 
 
 
 
Search WWH ::




Custom Search