Jump to: navigation, search

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

m (Locking)
(Locking)
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=Locking=
+
{{EclipseLink_UserGuide
You have the choice between optimistic and pessimistic locking. We recommend using EclipseLink [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Locking/Optimistic_Locking|optimistic locking]].
+
|info=y
 
+
|toc=n
By default, EclipseLink persistence provider assumes that the application is responsible for data consistency.
+
|eclipselink=y
 
+
|eclipselinktype=JPA
Use the <tt>@Version</tt> 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).
+
|examples=y
 
+
|example = *[[EclipseLink/Examples/JPA/Locking|Locking]]
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 (see Section 9.1.1 "Table Annotation" of the [http://jcp.org/en/jsr/detail?id=220 JPA Specification]);
+
* 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.
+
 
}}
 
}}
  
 +
=Locking=
 +
This section describes choices you need to make when deciding on how to use EclipseLink locking options in your application architecture. We strongly recommend always using a locking policy in a concurrent system.
  
The <tt>@Version</tt> annotation does not have attributes.
+
These choices can be summarized as follows:
 
+
* [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Locking/Optimistic Locking|Optimistic Locking]]
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.
+
* Pessimistic Locking
 
+
<span id="Example 18-10"></span>
+
''''' Usage of @Version Annotation'''''
+
<source lang="java">
+
@Entity
+
public class Employee implements Serializable {
+
    ...
+
    @Version
+
    @Column(name="OPTLOCK")
+
    protected int getVersionNum() {
+
        return versionNum;
+
    }
+
    ...
+
}
+
</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]]).
+
  
For more information, see the following:
+
If you are building a three-tier application, be aware of how that architecture affects the way you use locking).
* Section 3.4 "Optimistic Locking and Concurrency" of the [http://jcp.org/en/jsr/detail?id=220 JPA Specification]
+
* Section 9.1.17 "Version Annotation" of the [http://jcp.org/en/jsr/detail?id=220 JPA Specification]
+
* [[Using%20EclipseLink%20JPA%20Extensions%20(ELUG)#Using EclipseLink JPA Extensions for Optimistic Locking|Using EclipseLink JPA Extensions for Optimistic Locking]]
+
  
For more information on the EclipseLink artifacts configured by these JPA metadata, refer to [[Introduction%20to%20Descriptors%20(ELUG)#Descriptors and Locking|Descriptors and Locking]].
 
  
  
  
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA
|previous= [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings/ManyToMany|ManyToMany]]
+
|previous= [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings/ManyToMany|Many-to-Many Mapping]]
 
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Locking/Optimistic Locking|Optimistic Locking]]
 
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Locking/Optimistic Locking|Optimistic Locking]]
|up=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping|Mapping]]}}
+
|up=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping|Mapping]]
 +
|version=2.2.0 DRAFT}}

Latest revision as of 11:14, 25 April 2012

EclipseLink JPA

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

Elug example icon.png Examples


Locking

This section describes choices you need to make when deciding on how to use EclipseLink locking options in your application architecture. We strongly recommend always using a locking policy in a concurrent system.

These choices can be summarized as follows:

If you are building a three-tier application, be aware of how that architecture affects the way you use locking).



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