Troubleshooting JPA deadlocks and transaction time-outs

If an application encounters deadlocks and locked time-outs from the database, you can adjust the locking or transaction type to resolve the problem. Transactions are critical to maintaining data integrity. They are used to group operations into units of work that act together in an all-or-nothing fashion. There are two types of transactions, optimistic and pessimistic. Java™ Persistence API (JPA) for WebSphere® Application Server uses optimistic transactions by default, however, OpenJPA also supports pessimistic transactions. This topic includes the steps to configure these transactions.

Before you begin

Pessimistic transactions prevent all other concurrent transactions from using or modifying the same data by locking the datastore records that they are using. The advantage of a pessimistic transaction is that conflicts between multiple transactions are avoided. However, preventing these conflicts consumes more database resources than optimistic transactions. Because pessimistic transactions lock their records, it is possible that two transactions can wait for the other to release its lock before continuing. This results in a deadlock. The result is that one transaction is forced to unlock its records after a specified timeout interval and an exception is generated.

Optimistic transactions operate with the assumption that transactions do not conflict with each other. Optimistic transactions do not lock datastore records. Therefore, it is possible for two transactions to access and change the persistent information at the same time. Optimistic transactions consume less database resources than pessimistic transactions but they can be less reliable.

These changes are not detected until one transaction attempts to commit its changes. At that time, the transaction realizes that the data has been changed by another transaction and an OptimisticLockException exception displays. Despite this disadvantage, optimistic transactions are typically a better choice for applications because of performance and a low risk of deadlock.

Locking

Servers use objects called locks to maintain transaction and data integrity. Locks are managed automatically by lock management programs. Locking is handled on a per-transaction basis. When you obtain a lock on an entity, the lock indicates that you have the right to use the entity. The lock indicates that no other user can alter the data of the entity until the lock is released. Locking management ensures that no other user can obtain a lock that conflicts with a lock already in place. A lock cannot be used by more than one user. Locking management automatically assigns and releases locks, according the actions of the user.

To manage locks with JPA for WebSphere Application Server, you need to use the LockManager plug-in. The LockManager controls how the JPA application locks the data involved in the transaction. Depending on the setting used, LockManager determines if locking takes place, and if so, optimistic or pessimistic. In addition to LockManager, you need to control the level of isolation that the lock places on the entities and determine when the locking takes place in the transaction.

JPA for WebSphere Application Server contains advanced locking and versioning APIs for more control over database resources and object versions. The implementation works with the application server container for managed transactions. This allows the use of the declarative transaction demarcation and Java Transaction API (JTA) implementations within the container.

JPA Access Intent

JPA Access Intent in JPA for WebSphere Application Server provides an application improved control over the settings of isolation level and read lock when accessing data. This ability allows the data access criteria to be set based on the entity use and the taskName associated to the current transaction. By refining the scope of access, the deadlock condition is further minimized.

About this task

The following steps show you how to enable optimistic or pessimistic locking:

Procedure

Results

For more information, consult chapter 9 of the JPA Reference guide.



In this information ...


IBM Redbooks, demos, education, and more

(Index)

Use IBM Suggests to retrieve related content from ibm.com and beyond, identified for your convenience.

This feature requires Internet access.

Task topic    

Terms of Use | Feedback

Last updated: Oct 20, 2010 11:50:58 PM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=compass&product=was-base-iseries&topic=tejb_jpadeadlock
File name: tejb_jpadeadlock.html