OverviewThe pessimistic locking protocol provides data consistency in a multi user transactional environment. It should be used when there might be a large number of clients trying to read and update the same object(s) at the same time. This protocol utilize the system resources (CPU, network) in a very efficient manner both at the client and space server side. This scenario is different from the optimistic locking protocol since we assume with the pessimistic locking protocol, that every object that is read and retrieved from the space will eventually be updated where the transaction duration is relatively very short. To enforce a sole reader and sole updater for the object we explicitly lock the object via a transaction using the ReadModifiers.EXCLUSIVE_READ_LOCK . When performing read operations without locking the object via a transaction, users can immediately perform an update operation, without having to wait for other users to complete their transaction (since there is none). However, there is no guarantee that the update operation will be performed on the latest version of the object.
To implement the pessimistic locking protocol you should have the following:
ExampleSee below example illustrating the usage of the exclusive locking mode implementing pessimistic locking: @Transactional (propagation=Propagation.REQUIRES_NEW) public void executeOrders(Integer orderIDs[]) throws Exception { Order orders[] = new Order [orderIDs[].length]; for (int i=0;i<orderIDs.length;i++) { orders[i]= space.readById(Order.class, orderIDs[i],orderIDs[i],5000,ReadModifiers.EXCLUSIVE_READ_LOCK); if (orders[i] != null) orders[i].setStatus(DONE); } Object rets[] = space.updateMultiple(orders, new long[orderIDs.size()], UpdateModifiers.UPDATE_ONLY); for (int i=0;i<rets.length;i++) { if (rets[i] == null) { throw (new ReadTimeOutException("can't update object " + orders[i])); } if (rets[i] instanceof Exception) { if (rets[i] instanceof EntryNotInSpaceException) { throw (EntryNotInSpaceException)rets[i]; } else if (rets[i] instanceof OperationTimeoutException ) { throw (OperationTimeoutException)rets[i]; } else { throw (Exception)rets[i]; } } } } Important Considerations
|
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |