Local database reading edition

OPTIMISTIC AND PESSIMISTIC CONCURRENCY IN EJB

Reconstructed from database records, not a captured original forum layout. Dates and display names are retained as recorded; timezone and real-world identity are not inferred. Historical advice, contact details and offers are not current. Original markup is shown as inert text.

Forum index · Java - Distributed Component Design & Development

Opening post

Asha · 2001-05-22 12:20:36

To be an effective enterprise architect, you can't really rely on the J2EE API's to hide the complexities of distributed programming for you. A good architect should understand what goes on under the hood of an application server, inorder to make more educated design decisions on projects. An important and very confusing part of enterprise computing, or rather, transaction processing, is optimistic and pessimistic concurrency. Optimistic and Pessimistic concurrency are algorithms used to control concurrent access to the same data. These concepts come from database theory, where databases can use optimistic or pessimistic concurrency to control muliple transactions accessing the same data. In a nutshell, pessimistic concurrency algorithms allow only one transaction at a time to touch a particular source of data (a row in a database, an entity bean, etc). Other transactions that want access to the same data will block (wait) until the first transaction has completed. Optimistic concurrency doesn't enforce any such blocking. In an optimistic concurrency control scenario, multiple transactions can all access the same data concurrently, but if the consistency of the data accessed is compromised, one or more transactions will be rolled back. Application servers can be said to utilize pessimistic or optimistic concurrency if they choose to control the concurrent access to data (entity beans). More specifically, an application server can choose to enforce transaction isolation itself, or they can delegate this complex logic to the underlying datastore. An application server that delegates the transaction control to the database is not really using pessimistic or optimistic concurrency, it is simply delegating this task to the database. Application servers typically do this by allowing multiple intstances of entity beans in memory all with *the same primary key*. Now, when n transactions require usage of a particular entity bean, the application server will create n instances of the entity bean to service those n transactions. An application server that will block an entity bean from being read by some transaction if some other transaction is currently working with that entity bean is explicitly managing concurrent access to the entity bean (and the data it represents), and can thus be said to use pessimistic concurrency. These types of application servers usually enforce pessimistic concurrency by having only one entity bean in-memory, per primary key. Serializing access to this entity bean then becomes easy. Why should EJB architects understand the differences between these two algorithms? The answer is that if you don't, you risk writing code that could potentially become destructive when the applications database or application server is changed. So how does pessimistic and optimistic concurrency affect the behaviour of your EJB's? Assume two clients both have access to the same entity bean, that is, two clients (in two different transactions) both call findByPrimaryKey(123). If pessimistic concurrency (at the application server or database level) is used, then the first client who called findByPrimaryKey(123) will have exclusive access to work with that entity bean (he will have a lock on the bean, or the row in the database that the bean represents), while the second client will have to wait until the first client's transaction has committed, before he can use the bean. If Optimistic Concurrency is used (in the database level), then both clients will be able to access the entity bean at the same time. From this simplistic view, it looks like Optimistic Concurreny is faster, so why don't all application servers just use it? The answer is that Optimistic Concurrency is a double edged sword. Imagine the following scenario: 1) Client A starts a transaction and reads some data from a Bank Account entity bean with a balance of $10,000. 2) Client B does the same (since the data isn't locked), on the same entity bean. 3) Client A increments the bank balance by $2000 4) Client B does the same. The final balance in the entity bean will now be $12,000, not $14,000, because when client B's instance of the Bank Account entity bean executes ejbStore, it will write its new balance of $12,000 overwriting the $12,000 balance previously stored by client A. In this scenario, the underlying database will rolled back Client B's transaction, when it tried to commit. The application server would throw an exception to the client indicating that the transaction wsa rolled back. This means that EJB Developers would have to write custom logic to deal with this event, inorder to notify the client and ask him to retry his transaction. If Pessimistic Concurrency were used, Client B would have to wait until Client A finished using that particular Bank Account Entity Bean, and this problem would never have occurred. The EJB developer using an application server with Pessimistic Concurrency would therefore not have to write the extra exception handling code. Pessimistic Concurrency is "pessimistic" in that it assumes that these types of transaction "collisions" will occur often, and thus allows lower performance in exchange for stricter, cleaner transaction handling. Optimistic Concurrency is "optimistic" in that read only transactions are assumed to happen more often than update transactions, and thus chooses to support better performance for the majority of cases, the trade off being more complicated design and code on the part of the EJB Developer, inorder to deal with collisions. Almost every application server relies on the database's concurrency strategy (and allows multiple entity bean instances per primary key) (Inrpise, Oracle, Powertier, Gemstone, etc), but there are notable exceptions like BEA Weblogic, which uses Pessimistic Concurrency by default (and enforces one entity bean per primary key). In order to truly write portable code, it is important for EJB architects/developers to understand the difference between Pessimistic and Optimistic Concurrency, and know which application server/database combination use which algorithm. In many cases, code that assumed pessimistic concurrency will not be portable to application servers that use Optimistic Concurrency. A good example is the Entity Bean Primary Key Generator Pattern that I posted in TheServerSide.com patterns section (http://theserverside.com/patterns/thread.jsp?thread_id=220). The pattern assumed that an application server will only keep one instance of a particular entity bean in memory at a time. And that all access will be serialized. The pattern was written and tested on Weblogic, and I simply didn't know that other application server/databases handled entity beans in such a fundamentally different way. So which algorithm is better? That is a very tough call, since each has its own advantages. My own opinion is that Optimistic Concurrency is a better choice, due to the performance enhancements. What about the extra code you will have to write? Any serious eCommerce application will have to have "staleness checks" embedded in your entity beans anyway, since your entity beans will need to know when they are being updated by a user who is performing the update based on "stale data". This is also known as the dirty read problem. Therefore, since most EJB projects will need "dirty read" handling code anyway, then the potential performance bottlenecks of Pessimistic Concurrency no longer seem justified. /Content provided by Floyd Marinescu/ Asha Balasubramanyan http://www.ammu.org

Source notes

MyCommunity/nandighosha.org.zip!/forum/tools/snitz_forums_2000.mdb
Table: FORUM_TOPICS + FORUM_REPLY · record IDs: 186