Difference between revisions of "EclipseLink/Examples/JPA/Locking"

From Eclipsepedia

Jump to: navigation, search
m
 
Line 4: Line 4:
 
*[[#Optimistic Locking|Optimistic Locking]]
 
*[[#Optimistic Locking|Optimistic Locking]]
  
==Pessesmistic Locking ==
+
==Pessimistic Locking ==
 
With pessimistic locking, the first user who accesses the data with the purpose of updating it locks the data until completing the update. The disadvantage of this approach is that it may lead to reduced concurrency and deadlocks.
 
With pessimistic locking, the first user who accesses the data with the purpose of updating it locks the data until completing the update. The disadvantage of this approach is that it may lead to reduced concurrency and deadlocks.
  
Line 28: Line 28:
 
<source lang="java">
 
<source lang="java">
 
@Entity
 
@Entity
public class Empoyee implements Serializable {
+
public class Employee implements Serializable {
 
   ...
 
   ...
 
   @Version
 
   @Version

Latest revision as of 07:16, 30 April 2011

Contents

[edit] How to Use EclipseLink Locking

You can configure a descriptor with a locking policy that prevents one user writing over another user's work.

[edit] Pessimistic Locking

With pessimistic locking, the first user who accesses the data with the purpose of updating it locks the data until completing the update. The disadvantage of this approach is that it may lead to reduced concurrency and deadlocks.

See, PessimisticLocking

[edit] 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.

By default, EclipseLink persistence provider assumes that the application is responsible for data consistency.

EclipseLink also supports several extended locking policies and options in addition to JPA Version locking, see Using EclipseLink JPA Extensions for Optimistic Locking.

[edit] Version Locking

Optimistic version locking policies enforce optimistic locking by using a version field (also known as a write-lock field) that you provide in the reference class that EclipseLink updates each time an object change is committed.

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).

EclipseLink provides the following version-based optimistic locking policies:

  • VersionLockingPolicy: requires a numeric version field; EclipseLink updates the version field by incrementing its value by one.
  • TimestampLockingPolicy: requires a timestamp version field; EclipseLink updates the version field by inserting a new timestamp (this policy can be configured to get the time from the data source or locally; by default, the policy gets the time from the data source).

Optimistic Version Locking Example

@Entity
public class Employee implements Serializable {
  ...
  @Version
  @Column(name="VERSION")
  protected int getVersion() {
      return version;
  }
  ...

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.