Chapter 15. Optimistic Transactions

Earlier in this book, we discussed using datastore transactions to guarantee the following properties: atomicity, consistency, isolation, and durability. All operations between begin( ) and commit( ) of a JDO transaction are performed in the context of an underlying datastore transaction.

The datastore transaction model assumes that the duration of JDO transactions is relatively short. For longer transactions, JDO defines optimistic transactions, in which some of the transaction properties are implemented by JDO instead of the datastore.

Optimistic transactions are most useful for long-running transactions that rarely affect the same instances. These applications exhibit higher performance and better concurrency by deferring datastore locking on modified instances until commit. Whether you use optimistic or datastore transactions for your applications is a complex issue, because if there is significant contention for transactional instances, optimistic transactions can be less efficient than datastore transactions.

For example, JDO transactions performed in an application server with very high throughput and high concurrency are probably best implemented as datastore transactions. However, if JDO transactions include user "think time," then optimistic transactions are a good choice. The changes made to the cache might be made over a long period of time, during which no locks associated with any of the retrieved instances will be held in the datastore.

In the following summary, "transactional datastore context" refers to the transaction context of the underlying datastore, while "transaction," "datastore transaction," and "optimistic transaction" refer to the JDO transaction concepts.

JDO datastore transactions perform all datastore operations using the same transactional datastore context, which the JDO application delimits using the JDO Transaction methods. Thus, persistent instances accessed within the scope of an active JDO transaction are guaranteed to be associated with the transactional datastore context.

Prior to commit, JDO optimistic transactions perform all datastore operations using short transactional datastore contexts. Thus, persistent instances accessed within the scope of an active JDO transaction prior to commit are only briefly associated with a transactional datastore context. At JDO transaction commit, a transactional datastore context is used to perform all datastore modification operations.