124 Transaction management 7.3.3.2 Reacting to optimistic concurrency

124 Transaction management 7.3.3.2 Reacting to optimistic concurrency assumption failures The premise of optimistic transactions is that the data probably will not change during the transaction. The risk of this happening is justified by the increased concurrency afforded by an optimistic strategy. Occasionally, however, the data associated with an optimistic transaction is changed by another (optimistic or pessimistic) transaction. The application must be able to cope with this. Highly evolved applications may attempt some form of data reconciliation, essentially merging changes with the now updated persistent instance values. However, such strategies are inherently complex, and their implementation depends heavily on the business domain being modeled. The simplest form of resolution involves telling the user their changes could not be saved and must be done again. The instances are refreshed from the data store, and these values are presented to the user. 7.3.3.3 Example without explicit refresh Before we consider the mechanics of such a policy, let us closely examine the intricacies of optimistic transaction failure. For this purpose, presume that we are working with the Product class. Our transaction will update the description field. To start with, presume we have two products: one with a description of Green Chairs and the other Green Tables. We are going to examine the potential conflict of two transactions. For ease of reference we will refer to two users, User1 and User2, although the issue also pertains to situations where a single user has multiple transactions simultaneously active through different persistence managers in the same JVM. User1 starts an optimistic transaction within which both products, Green Chairs and Green Tables, are read. The instances will transition Persistent- Nontransactional at this point since they have been read in an optimistic transaction. User1 now goes to lunch without completing the transaction. User2 begins a new transaction optimistic or pessimistic, it doesn t matter which. Both products are read and their descriptions updated to Red Chairs and Red Tables respectively. The transaction is committed, and the commit is successful. The persistent data has now been changed. User1, having returned from lunch, changes the description of the second product to Blue Tables. That instance transitions to Persistent-Dirty. By pressing the Save button (imagine a carefully crafted Swing interface if you will!), the transaction commit is attempted. However we know that the transaction will fail, as the underlying data store entities have been altered by another transaction. According to the state transition tables of the JDO specification, the dirty instance will transition back to Hollow. Note that, according to the same state tables, the unaltered instance remains in the Persistent-Nontransactional state.

Hint: This post is supported by Gama web hosting hrvatska services

Comments are closed.