EclipseLink/Examples/JPA/Locking

Revision as of 10:04, 13 March 2009 by Rick.sapir.oracle.com(Talk | contribs)(New page: '''In progress''' ==How to Use EclipseLink Locking== You can configure a descriptor with a locking policy that prevents one user writing over another user's work. *[[#Pessemistic Locking...)

How to Use EclipseLink Locking

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

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.

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

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