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
This commit is contained in:
Michael Dick 2010-08-14 19:39:01 +00:00
parent 212bfb1a4a
commit 4560ddf64f
4 changed files with 47 additions and 22 deletions

View File

@ -201,5 +201,16 @@ public abstract class SingleEMFTestCase
protected Log getLog() { protected Log getLog() {
return emf.getConfiguration().getLog("Tests"); 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 "";
}
} }

View File

@ -36,8 +36,7 @@ import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
public class TestEmLockMode extends SequencedActionsTest { public class TestEmLockMode extends SequencedActionsTest {
private static String NON_SUPPORTED_OPTIMISTIC_SQL = private static String NON_SUPPORTED_OPTIMISTIC_SQL =
"SELECT .* FROM LockEmployee .*"; "SELECT .* FROM LockEmployee .*";
private static String NON_SUPPORTED_FOR_UPDATE_SQL = private static String NON_SUPPORTED_FOR_UPDATE_SQL = "" ; // append lock clause from dict
"SELECT .* FROM LockEmployee .* FOR UPDATE.*";
private static String VERSION_UPDATE_SQL = private static String VERSION_UPDATE_SQL =
"UPDATE LockEmployee SET version .* WHERE .*"; "UPDATE LockEmployee SET version .* WHERE .*";
private static String DB2_OPTIMISTIC_SQL = private static String DB2_OPTIMISTIC_SQL =
@ -50,6 +49,15 @@ public class TestEmLockMode extends SequencedActionsTest {
public void setUp() { public void setUp() {
setUp(LockEmployee.class, "openjpa.LockManager", "mixed"); setUp(LockEmployee.class, "openjpa.LockManager", "mixed");
commonSetUp(); 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;
} }
/* /*

View File

@ -37,23 +37,26 @@ import org.apache.openjpa.persistence.test.SQLListenerTestCase;
* *
*/ */
public class TestNamedQueryLockMode extends SQLListenerTestCase { public class TestNamedQueryLockMode extends SQLListenerTestCase {
String lockClause = "FOR UPDATE";
public void setUp() { public void setUp() {
super.setUp(CLEAR_TABLES, LockEmployee.class, super.setUp(CLEAR_TABLES, LockEmployee.class,
"openjpa.LockManager", "pessimistic", "openjpa.LockManager", "pessimistic",
"openjpa.Optimistic", "false" "openjpa.Optimistic", "false"
); );
lockClause = getForUpdateClause();
} }
public void testForUpdateClausePresentInNamedQueryWithLockMode() { public void testForUpdateClausePresentInNamedQueryWithLockMode() {
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); 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().rollback();
em.getTransaction().begin(); 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().rollback();
em.getTransaction().begin(); 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().rollback();
} }
@ -72,8 +75,8 @@ public class TestNamedQueryLockMode extends SQLListenerTestCase {
public void testForUpdateClausePresentInQueryWithDefault() { public void testForUpdateClausePresentInQueryWithDefault() {
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeById").setParameter("id", 0)); assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0));
assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeById").setParameter("id", 0)); assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0));
em.getTransaction().commit(); em.getTransaction().commit();
} }
@ -84,12 +87,12 @@ public class TestNamedQueryLockMode extends SQLListenerTestCase {
OpenJPAQuery<?> q = em.createNamedQuery("findEmployeeById").setParameter("id", 0); OpenJPAQuery<?> q = em.createNamedQuery("findEmployeeById").setParameter("id", 0);
FetchPlan fp = q.getFetchPlan(); FetchPlan fp = q.getFetchPlan();
fp.setReadLockMode(LockModeType.NONE); fp.setReadLockMode(LockModeType.NONE);
assertClauseAbsentInSQL("FOR UPDATE", q); assertClauseAbsentInSQL(lockClause, q);
q = em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0); q = em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0);
fp = q.getFetchPlan(); fp = q.getFetchPlan();
fp.setReadLockMode(LockModeType.NONE); fp.setReadLockMode(LockModeType.NONE);
assertClauseAbsentInSQL("FOR UPDATE", q); assertClauseAbsentInSQL(lockClause, q);
em.getTransaction().commit(); em.getTransaction().commit();
} }
@ -98,8 +101,8 @@ public class TestNamedQueryLockMode extends SQLListenerTestCase {
public void testForUpdateClauseAbsentInQueryWithExplictNoLock() { public void testForUpdateClauseAbsentInQueryWithExplictNoLock() {
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0));
assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0));
em.getTransaction().commit(); em.getTransaction().commit();
} }

View File

@ -22,12 +22,12 @@ import javax.persistence.EntityManager;
import javax.persistence.LockModeType; import javax.persistence.LockModeType;
import javax.persistence.Query; 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.FetchPlan;
import org.apache.openjpa.persistence.OpenJPAEntityManager; import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
import org.apache.openjpa.persistence.OpenJPAQuery; 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; 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. * query when Optimistic=true (default), with pessimistic lock manager.
*/ */
public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase { public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase {
String lockClause = "FOR UPDATE";
public void setUp() { public void setUp() {
super.setUp(CLEAR_TABLES, LockEmployee.class, super.setUp(CLEAR_TABLES, LockEmployee.class,
"openjpa.LockManager", "pessimistic" "openjpa.LockManager", "pessimistic"
); );
lockClause = getForUpdateClause();
} }
/* /*
@ -47,13 +50,13 @@ public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase {
public void testForUpdateClausePresentInNamedQueryWithLockMode() { public void testForUpdateClausePresentInNamedQueryWithLockMode() {
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); 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().rollback();
em.getTransaction().begin(); 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().rollback();
em.getTransaction().begin(); 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().rollback();
em.close(); em.close();
} }
@ -64,8 +67,8 @@ public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase {
public void testForUpdateClausePresentInQueryWithDefault() { public void testForUpdateClausePresentInQueryWithDefault() {
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeById").setParameter("id", 0)); assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0));
assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeById").setParameter("id", 0)); assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0));
OpenJPAEntityManager oem = (OpenJPAEntityManager)em; OpenJPAEntityManager oem = (OpenJPAEntityManager)em;
OpenJPAQuery<?> q = oem.createNamedQuery("findEmployeeById").setParameter("id", 0); 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); OpenJPAQuery<?> q = em.createNamedQuery("findEmployeeById").setParameter("id", 0);
FetchPlan fp = q.getFetchPlan(); FetchPlan fp = q.getFetchPlan();
fp.setReadLockMode(LockModeType.NONE); fp.setReadLockMode(LockModeType.NONE);
assertClauseAbsentInSQL("FOR UPDATE", q); assertClauseAbsentInSQL(lockClause, q);
q = em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0); q = em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0);
fp = q.getFetchPlan(); fp = q.getFetchPlan();
fp.setReadLockMode(LockModeType.NONE); fp.setReadLockMode(LockModeType.NONE);
assertClauseAbsentInSQL("FOR UPDATE", q); assertClauseAbsentInSQL(lockClause, q);
em.getTransaction().commit(); em.getTransaction().commit();
em.close(); em.close();
@ -103,8 +106,8 @@ public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase {
public void testForUpdateClauseAbsentInQueryWithExplictNoLock() { public void testForUpdateClauseAbsentInQueryWithExplictNoLock() {
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0));
assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0));
em.getTransaction().commit(); em.getTransaction().commit();
em.close(); em.close();
} }