From 4560ddf64fd5150b1fcb0ac7f9b53deaae3e09f1 Mon Sep 17 00:00:00 2001 From: Michael Dick Date: Sat, 14 Aug 2010 19:39:01 +0000 Subject: [PATCH] get lock clause from DBDictionary git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.0.x@985574 13f79535-47bb-0310-9956-ffa450edef68 --- .../persistence/test/SingleEMFTestCase.java | 11 ++++++++ .../persistence/lockmgr/TestEmLockMode.java | 12 +++++++-- .../lockmgr/TestNamedQueryLockMode.java | 21 +++++++++------- .../TestOptimisticNamedQueryLockMode.java | 25 +++++++++++-------- 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java index 4adc5cbc2..5b141f048 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java @@ -201,5 +201,16 @@ public abstract class SingleEMFTestCase protected Log getLog() { return emf.getConfiguration().getLog("Tests"); } + + protected String getForUpdateClause() { + DBDictionary dict = ((JDBCConfiguration) emf.getConfiguration()).getDBDictionaryInstance(); + if (dict.forUpdateClause != null) { + return dict.forUpdateClause; + } + if (dict.tableForUpdateClause != null) { + return dict.tableForUpdateClause; + } + return ""; + } } diff --git a/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestEmLockMode.java b/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestEmLockMode.java index 521d9e407..daee91e13 100644 --- a/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestEmLockMode.java +++ b/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestEmLockMode.java @@ -36,8 +36,7 @@ import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; public class TestEmLockMode extends SequencedActionsTest { private static String NON_SUPPORTED_OPTIMISTIC_SQL = "SELECT .* FROM LockEmployee .*"; - private static String NON_SUPPORTED_FOR_UPDATE_SQL = - "SELECT .* FROM LockEmployee .* FOR UPDATE.*"; + private static String NON_SUPPORTED_FOR_UPDATE_SQL = "" ; // append lock clause from dict private static String VERSION_UPDATE_SQL = "UPDATE LockEmployee SET version .* WHERE .*"; private static String DB2_OPTIMISTIC_SQL = @@ -50,6 +49,15 @@ public class TestEmLockMode extends SequencedActionsTest { public void setUp() { setUp(LockEmployee.class, "openjpa.LockManager", "mixed"); commonSetUp(); + NON_SUPPORTED_FOR_UPDATE_SQL = NON_SUPPORTED_OPTIMISTIC_SQL + " " + escapeRegex(getForUpdateClause()) + ".*"; + } + + private String escapeRegex(String clause) { + // escape an update clause for use in a regex. + // only handling ( ) for now + String rval = clause.replace("(", "\\("); + rval = rval.replace(")", "\\)"); + return rval; } /* diff --git a/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestNamedQueryLockMode.java b/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestNamedQueryLockMode.java index 2603153d0..e67fd6cf0 100644 --- a/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestNamedQueryLockMode.java +++ b/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestNamedQueryLockMode.java @@ -37,23 +37,26 @@ import org.apache.openjpa.persistence.test.SQLListenerTestCase; * */ public class TestNamedQueryLockMode extends SQLListenerTestCase { + String lockClause = "FOR UPDATE"; + public void setUp() { super.setUp(CLEAR_TABLES, LockEmployee.class, "openjpa.LockManager", "pessimistic", "openjpa.Optimistic", "false" ); + lockClause = getForUpdateClause(); } public void testForUpdateClausePresentInNamedQueryWithLockMode() { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); - assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); + assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); em.getTransaction().rollback(); em.getTransaction().begin(); - assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); + assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); em.getTransaction().rollback(); em.getTransaction().begin(); - assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); + assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); em.getTransaction().rollback(); } @@ -72,8 +75,8 @@ public class TestNamedQueryLockMode extends SQLListenerTestCase { public void testForUpdateClausePresentInQueryWithDefault() { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); - assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeById").setParameter("id", 0)); - assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeById").setParameter("id", 0)); + assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0)); + assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0)); em.getTransaction().commit(); } @@ -84,12 +87,12 @@ public class TestNamedQueryLockMode extends SQLListenerTestCase { OpenJPAQuery q = em.createNamedQuery("findEmployeeById").setParameter("id", 0); FetchPlan fp = q.getFetchPlan(); fp.setReadLockMode(LockModeType.NONE); - assertClauseAbsentInSQL("FOR UPDATE", q); + assertClauseAbsentInSQL(lockClause, q); q = em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0); fp = q.getFetchPlan(); fp.setReadLockMode(LockModeType.NONE); - assertClauseAbsentInSQL("FOR UPDATE", q); + assertClauseAbsentInSQL(lockClause, q); em.getTransaction().commit(); } @@ -98,8 +101,8 @@ public class TestNamedQueryLockMode extends SQLListenerTestCase { public void testForUpdateClauseAbsentInQueryWithExplictNoLock() { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); - assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); - assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); + assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); + assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); em.getTransaction().commit(); } diff --git a/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestOptimisticNamedQueryLockMode.java b/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestOptimisticNamedQueryLockMode.java index 7710fd51e..2850dd998 100644 --- a/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestOptimisticNamedQueryLockMode.java +++ b/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestOptimisticNamedQueryLockMode.java @@ -22,12 +22,12 @@ import javax.persistence.EntityManager; import javax.persistence.LockModeType; import javax.persistence.Query; +import org.apache.openjpa.jdbc.conf.JDBCConfiguration; +import org.apache.openjpa.jdbc.sql.DBDictionary; import org.apache.openjpa.persistence.FetchPlan; import org.apache.openjpa.persistence.OpenJPAEntityManager; import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; import org.apache.openjpa.persistence.OpenJPAQuery; -import org.apache.openjpa.persistence.TransactionRequiredException; -import org.apache.openjpa.persistence.test.AllowFailure; import org.apache.openjpa.persistence.test.SQLListenerTestCase; /** @@ -35,10 +35,13 @@ import org.apache.openjpa.persistence.test.SQLListenerTestCase; * query when Optimistic=true (default), with pessimistic lock manager. */ public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase { + String lockClause = "FOR UPDATE"; + public void setUp() { super.setUp(CLEAR_TABLES, LockEmployee.class, "openjpa.LockManager", "pessimistic" ); + lockClause = getForUpdateClause(); } /* @@ -47,13 +50,13 @@ public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase { public void testForUpdateClausePresentInNamedQueryWithLockMode() { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); - assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); + assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); em.getTransaction().rollback(); em.getTransaction().begin(); - assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); + assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); em.getTransaction().rollback(); em.getTransaction().begin(); - assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); + assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); em.getTransaction().rollback(); em.close(); } @@ -64,8 +67,8 @@ public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase { public void testForUpdateClausePresentInQueryWithDefault() { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); - assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeById").setParameter("id", 0)); - assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeById").setParameter("id", 0)); + assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0)); + assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0)); OpenJPAEntityManager oem = (OpenJPAEntityManager)em; OpenJPAQuery q = oem.createNamedQuery("findEmployeeById").setParameter("id", 0); @@ -86,12 +89,12 @@ public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase { OpenJPAQuery q = em.createNamedQuery("findEmployeeById").setParameter("id", 0); FetchPlan fp = q.getFetchPlan(); fp.setReadLockMode(LockModeType.NONE); - assertClauseAbsentInSQL("FOR UPDATE", q); + assertClauseAbsentInSQL(lockClause, q); q = em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0); fp = q.getFetchPlan(); fp.setReadLockMode(LockModeType.NONE); - assertClauseAbsentInSQL("FOR UPDATE", q); + assertClauseAbsentInSQL(lockClause, q); em.getTransaction().commit(); em.close(); @@ -103,8 +106,8 @@ public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase { public void testForUpdateClauseAbsentInQueryWithExplictNoLock() { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); - assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); - assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); + assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); + assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); em.getTransaction().commit(); em.close(); }