Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Locking/Optimistic Locking"

Line 5: Line 5:
|apis=[http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Version.html @Version]
*[http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Version.html @Version]
|example = *[[EclipseLink/Examples/JPA/Locking|Locking]]
|example =
=Optimistic Locking=
=Optimistic Locking=

Revision as of 21:42, 30 March 2011

EclipseLink JPA

Mailing ListForumsIRC
Help Wanted
Bug Day
Browse Source

Elug api package icon.png Key API

Elug example icon.png Examples

Optimistic Locking

We recommend using EclipseLink optimistic locking. With optimistic locking, all users have read access to the data. When a user attempts to write a change, the application checks to ensure the data has not changed since the user read the data.

Optimistic locking in a stateless environmentIn a stateless environment, take care to avoid processing out-of-date (stale) data A common strategy for avoiding stale data is to implement optimistic locking, and store the optimistic lock values in the object. This solution requires careful implementation if the stateless application serializes the objects, or sends the contents of the object to the client in an alternative format. In this case, transport the optimistic lock values to the client in the HTTP contents of an edit page. You must then use the returned values in any write transaction to ensure that the data did not change while the client was performing its work.

You can use Optimistic Version Locking or Optimistic Field Locking locking policies. We recommend using version locking policies.

Optimistic Version Locking

Use the @Version annotation to enable the JPA-managed optimistic locking by specifying the version field or property of an entity class that serves as its optimistic lock value (recommended).

When choosing a version field or property, ensure that the following is true:

  • there is only one version field or property per entity;
  • you choose a property or field persisted to the primary table;
Elug javaspec icon.gif

For more information, see Section 9.1.1 "Table Annotation" in the JPA Specification.

  • your application does not modify the version property or field.

Elug note icon.png

Note: The field or property type must either be a numeric type (such as Number, long, int, BigDecimal, and so on), or a java.sql.Timestamp. We recommend using a numeric type.

The @Version annotation does not have attributes.

The following example shows how to use this annotation to specify property getVersionNum as the optimistic lock value. In this example, the column name for this property is set to OPTLOCK instead of the default column name for the property..

Elug javaspec icon.gif

For more information, see Section 9.1.5 "Column Annotation" in the JPA Specification.

Usage of @Version Annotation
 public class Employee implements Serializable {
     protected int getVersionNum() {
         return versionNum;

The @Version annotation supports the use of EclipseLink converters.

Version: 2.2.0 DRAFT
Other versions...