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

From Eclipsepedia

Jump to: navigation, search
(Via XML)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
EclipseLink allows users the option of using pessimistic locking on their queries. This is done through the use of a query hint.
+
[[Category:EclipseLink/Example/JPA|PessimisticLocking]]
 +
 
 +
EclipseLink allows users the option of using pessimistic locking on their queries. In JPA 1.0 this can be done through the use of a query hint.
 +
 
 +
Pessimistic locking is also supported in JPA 2.0 using the LockMode on a Query.
 +
 
 +
See:
 +
* org.eclipse.persistence.config.QueryHints
 +
* org.eclipse.persistence.config.PessimisticLock
 +
* [[Configuring_a_Descriptor_%28ELUG%29#Configuring_Locking_Policy|Configuring a Locking Policy]] in the ''EclipseLink User's Guide''
  
 
== Via Annotations ==
 
== Via Annotations ==
<code><pre>
+
<source lang="java">
 
@Entity
 
@Entity
 
@Table(name="JPA_EMPLOYEE")
 
@Table(name="JPA_EMPLOYEE")
Line 10: Line 19:
 
   hints={
 
   hints={
 
     @QueryHint(
 
     @QueryHint(
       name=EclipseLinkQueryHints.PESSIMISTIC_LOCK,  
+
       name=QueryHints.PESSIMISTIC_LOCK,  
       value=EclipseLinkQueryHints.PESSIMISTIC_LOCK)
+
       value=PessimisticLock.Lock)
 
   }
 
   }
 
)
 
)
Line 18: Line 27:
  
 
}
 
}
</pre></code>
+
</source>
  
 
== Via XML ==
 
== Via XML ==
<code><pre>
+
<source lang="xml">
 
<entity name="Employee" class="org.eclipse.testing.Employee" access="PROPERTY">
 
<entity name="Employee" class="org.eclipse.testing.Employee" access="PROPERTY">
 
   ...
 
   ...
 
   <named-query name="findEmployeeByPK">
 
   <named-query name="findEmployeeByPK">
 
     <query>SELECT OBJECT(employee) FROM Employee employee WHERE employee.id = :id</query>
 
     <query>SELECT OBJECT(employee) FROM Employee employee WHERE employee.id = :id</query>
     <hint name="eclipselink.pessimistic-lock" value="eclipselink.pessimistic-lock">
+
     <hint name="eclipselink.pessimistic-lock" value="Lock">
 
   </named-query>
 
   </named-query>
 
   ...
 
   ...
 
</entity>
 
</entity>
</pre></code>
+
</source>

Latest revision as of 10:57, 15 June 2010


EclipseLink allows users the option of using pessimistic locking on their queries. In JPA 1.0 this can be done through the use of a query hint.

Pessimistic locking is also supported in JPA 2.0 using the LockMode on a Query.

See:

  • org.eclipse.persistence.config.QueryHints
  • org.eclipse.persistence.config.PessimisticLock
  • Configuring a Locking Policy in the EclipseLink User's Guide

[edit] Via Annotations

@Entity
@Table(name="JPA_EMPLOYEE")
@NamedQuery(
  name="findEmployeeByPK",
  query="SELECT OBJECT(employee) FROM Employee employee WHERE employee.id = :id"),
  hints={
    @QueryHint(
      name=QueryHints.PESSIMISTIC_LOCK, 
      value=PessimisticLock.Lock)
  }
)
public class Employee implements Serializable {
...
 
}

[edit] Via XML

<entity name="Employee" class="org.eclipse.testing.Employee" access="PROPERTY">
  ...
  <named-query name="findEmployeeByPK">
    <query>SELECT OBJECT(employee) FROM Employee employee WHERE employee.id = :id</query>
    <hint name="eclipselink.pessimistic-lock" value="Lock">
  </named-query>
  ...
</entity>