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

From Eclipsepedia

Jump to: navigation, search
m
m
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide|info=y|toc=y|examples=y
+
{{EclipseLink_UserGuide
|example = *[[EclipseLink/Examples/JPA/Locking|Locking]]
+
|info=y
 +
|toc=n
 +
|eclipselink=y
 +
|eclipselinktype=JPA
 +
|api=y
 +
|apis=
 +
*[http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Version.html @Version]
 +
|examples=y
 +
|example =
 +
*[[EclipseLink/Examples/JPA/Locking|Locking]]
 
}}
 
}}
 
=Optimistic Locking=
 
=Optimistic Locking=
Line 9: Line 18:
 
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.
 
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 [[Introduction%20to%20Descriptors%20(ELUG)#Optimistic Version Locking Policies|Optimistic Version Locking]] or [[Introduction%20to%20Descriptors%20(ELUG)#Optimistic Field Locking Policies|Optimistic Field Locking]] locking policies. We recommend using version locking policies.
+
You can use optimistic version locking or optimistic field Locking policies locking policies. We recommend using version locking policies.
  
 
==Optimistic Version Locking==
 
==Optimistic Version Locking==
Line 16: Line 25:
 
When choosing a version field or property, ensure that the following is true:
 
When choosing a version field or property, ensure that the following is true:
 
* there is only one version field or property per entity;
 
* there is only one version field or property per entity;
* you choose a property or field persisted to the primary table (see Section 9.1.1 "Table Annotation" of the [http://jcp.org/en/jsr/detail?id=220 JPA Specification]);
+
* you choose a property or field persisted to the primary table;{{EclipseLink_Spec|section=Section 11.1.45 "Table Annotation"}}
 
* your application does not modify the version property or field.
 
* your application does not modify the version property or field.
  
{EclipseLink_Note|note=The field or property type must either be a numeric type (such as <tt>Number</tt>, <tt>long</tt>, <tt>int</tt>, <tt>BigDecimal</tt>, and so on), or a <tt>java.sql.Timestamp</tt>. We recommend using a numeric type.
+
{{EclipseLink_Note|note=The field or property type must either be a numeric type (such as <tt>Number</tt>, <tt>long</tt>, <tt>int</tt>, <tt>BigDecimal</tt>, and so on), or a <tt>java.sql.Timestamp</tt>. We recommend using a numeric type.
 
}}
 
}}
  
Line 25: Line 34:
 
The <tt>@Version</tt> annotation does not have attributes.
 
The <tt>@Version</tt> annotation does not have attributes.
  
The [[#Example 18-10|Usage of @Version Annotation]] example shows how to use this annotation to specify property <tt>getVersionNum</tt> as the optimistic lock value. In this example, the column name for this property is set to <tt>OPTLOCK</tt> (see Section 9.1.5 "Column Annotation" of the [http://jcp.org/en/jsr/detail?id=220 JPA Specification]) instead of the default column name for the property.
+
The following example shows how to use this annotation to specify property <tt>getVersionNum</tt> as the optimistic lock value. In this example, the column name for this property is set to <tt>OPTLOCK</tt> instead of the default column name for the property..
 +
{{EclipseLink_Spec|section=Section 11.1.9 "Column Annotation"}}
  
<span id="Example 18-10"></span>
+
======'' Example: @Version Annotation''======
''''' Usage of @Version Annotation'''''
+
 
<source lang="java">
 
<source lang="java">
 
  @Entity
 
  @Entity
Line 42: Line 51:
 
</source>
 
</source>
  
The <tt>@Version</tt> annotation supports the use of EclipseLink converters (see [[Using%20EclipseLink%20JPA%20Extensions%20(ELUG)#Using EclipseLink JPA Converters|Using EclipseLink JPA Converters]]).
+
The <tt>@Version</tt> annotation supports the use of [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters|EclipseLink converters]].
  
  
Line 50: Line 59:
 
|previous= [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Locking|Locking]]
 
|previous= [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Locking|Locking]]
 
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching|Caching]]
 
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching|Caching]]
|up=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Locking|Locking]]}}
+
|up=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Locking|Locking]]
 +
|version=2.2.0 DRAFT}}

Latest revision as of 11:22, 4 May 2011

EclipseLink JPA

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

Elug api package icon.png Key API

Elug example icon.png Examples

[edit] 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 policies locking policies. We recommend using version locking policies.

[edit] 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 11.1.45 "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 11.1.9 "Column Annotation" in the JPA Specification.

[edit] Example: @Version Annotation
 @Entity
 public class Employee implements Serializable {
     ...
     @Version
     @Column(name="OPTLOCK")
     protected int getVersionNum() {
         return versionNum;
     }
     ...
 }

The @Version annotation supports the use of EclipseLink converters.



Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...