Java Reference
In-Depth Information
users might be less diligent in exiting the application gracefully and unlocking
orders. As a result, there is an increased chance of orders remaining locked.
An alternative approach is for the implementation of the Acknowledge Order
use case to continue to use the Optimistic Offline Lock pattern with some minor
enhancements. The AcknowledgeOrderService verifies that the order is unlocked
at the start of the use case and locks the order in the final database transaction
while updating it. The Optimistic Offline Lock pattern detects when an order was
changed by another transaction, and locking the order while updating it ensures
that it is safe to do so. This approach avoids having the restaurant user holding
onto long-term pessimistic offline locks but does make the code a little more com-
plex. Here is an excerpt of the code for the AcknowledgeOrderService , which
implements the business logic for this use case. The changes to the getOrderTo-
Acknowledge() and acknowledgeOrder() methods appear in bold.
public class DetachingAcknowledgeOrderServiceWithLockImpl implements
DetachingAcknowledgeOrderService {
public AcknowledgeOrderResult getOrderToAcknowledge(
String orderId) {
Order order = orderRepository.findOrder(orderId);
Order detachedOrder = attachmentManager.detach(order);
if (order.isAcknowledgable()) {
if (lockManager
.isLocked(Order.class,
order.getId()))
return new AcknowledgeOrderResult(
AcknowledgeOrderResult.LOCKED,
detachedOrder);
else
return new AcknowledgeOrderResult(
AcknowledgeOrderResult.OK, detachedOrder);
} else
return new AcknowledgeOrderResult(
AcknowledgeOrderResult.ILLEGAL_STATE,
detachedOrder);
}
Verifies order
not locked
public AcknowledgeOrderResult acknowledgeOrder(
Order detachedOrder, String owner ) {
if (!lockManager
.acquireLock(Order.getName(),
detachedOrder
.getId(),
owner))
return new AcknowledgeOrderResult(
AcknowledgeOrderResult.LOCKED, detachedOrder);
Locks order
 
Search WWH ::




Custom Search