made in-mem match queries fail faster; added another test case for new DataCacheStoreManager work

git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@469429 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Patrick Linskey 2006-10-31 09:43:43 +00:00
parent 8b2e032621
commit caa1ea4543
4 changed files with 118 additions and 23 deletions

View File

@ -578,10 +578,11 @@ public class DataCacheStoreManager
switch (compareVersion(sm, sm.getVersion(), data.getVersion())) {
case StoreManager.VERSION_LATER:
case StoreManager.VERSION_SAME:
// This tx's current version is later than the data cache
// version. In this case, the commit should have succeeded.
// Remove the instance from cache in the hopes that the
// cache is out of sync.
// This tx's current version is later than or the same as
// the data cache version. In this case, the commit should
// have succeeded from the standpoint of the cache. Remove
// the instance from cache in the hopes that the cache is
// out of sync.
remove = true;
break;
case StoreManager.VERSION_EARLIER:

View File

@ -15,6 +15,7 @@
*/
package org.apache.openjpa.kernel.exps;
import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.lib.util.SimpleRegex;
import serp.util.Strings;
@ -26,9 +27,11 @@ import serp.util.Strings;
class MatchesExpression
extends CompareExpression {
private static final Localizer _loc = Localizer.forPackage(
MatchesExpression.class);
private final String _single;
private final String _multi;
private final String _escape; // ### in-memory queries are not using escapes
private final boolean _affirmation;
/**
@ -39,7 +42,9 @@ class MatchesExpression
super(val1, val2);
_single = single;
_multi = multi;
_escape = escape;
if (escape != null)
throw new IllegalArgumentException(_loc.get(
"escape-for-inmem-query-not-supported").getMessage());
_affirmation = affirmation;
}

View File

@ -20,6 +20,7 @@ public class TestDataCacheOptimisticLockRecovery
extends TestCase {
private EntityManagerFactory emf;
private int pk;
public void setUp() {
Map options = new HashMap();
@ -38,7 +39,16 @@ public class TestDataCacheOptimisticLockRecovery
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from OptimisticLockInstance");
em.getTransaction().commit();
OptimisticLockInstance oli = new OptimisticLockInstance("foo");
try {
em.persist(oli);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
pk = oli.getPK();
em.close();
}
@ -51,26 +61,12 @@ public class TestDataCacheOptimisticLockRecovery
EntityManager em;
// 1. get the instance into the cache via this insert
em = emf.createEntityManager();
em.getTransaction().begin();
OptimisticLockInstance oli = new OptimisticLockInstance("foo");
try {
em.persist(oli);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
int pk = oli.getPK();
em.close();
// 2. get the oplock value for the instance after commit and
// 1. get the oplock value for the instance after commit and
// get a read lock to ensure that we check for the optimistic
// lock column at tx commit.
em = emf.createEntityManager();
em.getTransaction().begin();
oli = em.find(OptimisticLockInstance.class, pk);
OptimisticLockInstance oli = em.find(OptimisticLockInstance.class, pk);
int firstOpLockValue = oli.getOpLock();
em.lock(oli, LockModeType.READ);
@ -129,4 +125,31 @@ public class TestDataCacheOptimisticLockRecovery
}
em.close();
}
public void testExpectedOptimisticLockException() {
EntityManager em;
// 1. start a new tx
em = emf.createEntityManager();
em.getTransaction().begin();
em.lock(em.find(OptimisticLockInstance.class, pk), LockModeType.READ);
// 2. start another tx, and cause a version increment
EntityManager em2 = emf.createEntityManager();
em2.getTransaction().begin();
em2.lock(em2.find(OptimisticLockInstance.class, pk),
LockModeType.WRITE);
em2.getTransaction().commit();
em2.close();
// 3. try to commit. this should fail, as this is a regular optimistic
// lock failure situation.
try {
em.getTransaction().commit();
fail("write lock in em2 should trigger an optimistic lock failure");
} catch (RollbackException pe) {
// expected
}
em.close();
}
}

View File

@ -0,0 +1,66 @@
package org.apache.openjpa.persistence.query;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.FlushModeType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.openjpa.persistence.OpenJPAQuery;
import org.apache.openjpa.persistence.simple.AllFieldTypes;
import junit.framework.TestCase;
public class TestInMemoryQueryMatchEscapes
extends TestCase {
private EntityManagerFactory emf;
public void setUp() {
Map options = new HashMap();
// ensure that OpenJPA knows about our type, so that
// auto-schema-creation works
options.put("openjpa.MetaDataFactory",
"jpa(Types=" + AllFieldTypes.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", options);
}
public void testDatabaseEscape() {
OpenJPAQuery q = escapeHelper(false);
q.setFlushMode(FlushModeType.AUTO);
q.getEntityManager().flush();
AllFieldTypes aft = (AllFieldTypes) q.getSingleResult();
assertEquals("foo_bar", aft.getStringField());
q.getEntityManager().getTransaction().rollback();
}
public void testInMemoryEscape() {
OpenJPAQuery q = escapeHelper(true);
q.setFlushMode(FlushModeType.COMMIT);
try {
q.getSingleResult();
fail("OpenJPA doesn't support escape syntax for in-mem queries");
} catch (Exception e) {
// expected
}
}
private OpenJPAQuery escapeHelper(boolean inMem) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
AllFieldTypes aft = new AllFieldTypes();
aft.setStringField("foo_bar");
em.persist(aft);
aft = new AllFieldTypes();
aft.setStringField("foozbar");
em.persist(aft);
return (OpenJPAQuery) em.createQuery(
"select e from AllFieldTypes e where e.stringField " +
"like 'foox_bar' escape 'x'");
}
}