From afa1e44f81246425dfe9caee7fe7f4027f171b6f Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Wed, 7 Apr 2010 17:39:26 +0000 Subject: [PATCH] OPENJPA-1602: check in test case git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@931628 13f79535-47bb-0310-9956-ffa450edef68 --- .../lockmgr/TestPessimisticLocks.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestPessimisticLocks.java b/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestPessimisticLocks.java index f6d8d5147..6d2a75548 100644 --- a/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestPessimisticLocks.java +++ b/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestPessimisticLocks.java @@ -334,6 +334,31 @@ public class TestPessimisticLocks extends SQLListenerTestCase { em2.close(); } + /* + * Test multiple execution of the same query with pessimistic lock. + */ + public void testRepeatedQueryWithPessimisticLocks() { + EntityManager em = emf.createEntityManager(); + resetSQL(); + em.getTransaction().begin(); + String jpql = "select e.firstName from Employee e where e.id = 1"; + Query q1 = em.createQuery(jpql); + q1.setLockMode(LockModeType.PESSIMISTIC_WRITE); + String firstName1 = (String) q1.getSingleResult(); + //Expected sql for Derby is: + //SELECT t0.firstName FROM Employee t0 WHERE (t0.id = CAST(? AS BIGINT)) FOR UPDATE WITH RR + String SQL1 = toString(sql); + + // run the second time + resetSQL(); + Query q2 = em.createQuery(jpql); + q2.setLockMode(LockModeType.PESSIMISTIC_WRITE); + String firstName2 = (String) q2.getSingleResult(); + String SQL2 = toString(sql); + assertEquals(SQL1, SQL2); + em.getTransaction().commit(); + } + /** * Assert that an exception of proper type has been thrown. Also checks that * that the exception has populated the failed object.