Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

m (Locking)
m (Locking)
Line 1: Line 1:
 
=Locking=
 
=Locking=
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]].  
+
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 (see [[Configuring%20a%20Descriptor%20(ELUG)#Configuring Locking Policy|Configuring Locking Policy]]).
  
By default, EclipseLink persistence provider assumes that the application is responsible for data consistency.
+
These choices can be summarized as follows:
 +
* [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Locking/Optimistic Locking|Optimistic Locking]]
 +
* Pessimistic Locking
  
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).
+
If you are building a three-tier application, be aware of how that architecture affects the way you use locking (see [[Introduction%20to%20Descriptors%20(ELUG)#Locking in a Three-Tier Application|Locking in a Three-Tier Application]]).
  
When choosing a version field or property, ensure that the following is true:
+
For more information, see [[Introduction%20to%20Descriptors%20(ELUG)#Descriptors and Locking|Descriptors and Locking]].
* 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.
 
}}
 
 
 
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.
 
 
<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:
 
* 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]].
 
  
  

Revision as of 14:59, 15 June 2010

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 (see Configuring Locking Policy).

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 (see Locking in a Three-Tier Application).

For more information, see Descriptors and Locking.



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

Back to the top