OPENJPA-1599 - Push Cache*Mode value(s) in fetch configuration set from property making the property value(s) only valid during the em method call.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.0.x@927442 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Albert Lee 2010-03-25 15:09:00 +00:00
parent fb60f8136f
commit 1c2f4470a2
2 changed files with 91 additions and 9 deletions

View File

@ -18,15 +18,12 @@
*/ */
package org.apache.openjpa.persistence.simple; package org.apache.openjpa.persistence.simple;
import java.sql.Connection; import java.util.HashMap;
import java.sql.SQLException;
import java.util.Map; import java.util.Map;
import javax.persistence.CacheRetrieveMode; import javax.persistence.CacheRetrieveMode;
import javax.persistence.CacheStoreMode; import javax.persistence.CacheStoreMode;
import javax.persistence.EntityManager;
import org.apache.openjpa.kernel.Broker;
import org.apache.openjpa.persistence.JPAProperties; import org.apache.openjpa.persistence.JPAProperties;
import org.apache.openjpa.persistence.OpenJPAEntityManager; import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.test.SingleEMTestCase; import org.apache.openjpa.persistence.test.SingleEMTestCase;
@ -133,7 +130,93 @@ public class TestRefresh extends SingleEMTestCase {
} }
} }
public void testFindWithCacheRetrieveProperty() {
String key = "Test property in find.";
OpenJPAEntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Item item = new Item();
item.setItemData(key);
em.persist(item);
em.flush();
em.getTransaction().commit();
int id = item.getItemId();
em.clear();
emf.getCache().evictAll();
assertEquals(key, item.getItemData());
em.setProperty(JPAProperties.CACHE_STORE_MODE, CacheStoreMode.USE);
em.setProperty(JPAProperties.CACHE_RETRIEVE_MODE, CacheRetrieveMode.USE);
Map<String, Object> properties = em.getProperties();
if (!properties.containsKey(JPAProperties.CACHE_STORE_MODE)) {
System.err.println(properties);
fail("Expected " + JPAProperties.CACHE_STORE_MODE + " properties be returned");
}
if (!properties.containsKey(JPAProperties.CACHE_RETRIEVE_MODE)) {
System.err.println(properties);
fail("Expected " + JPAProperties.CACHE_RETRIEVE_MODE + " properties be returned");
}
Map<String, Object> paramProperties = new HashMap<String, Object>();
paramProperties.put(JPAProperties.CACHE_STORE_MODE, CacheStoreMode.BYPASS);
paramProperties.put(JPAProperties.CACHE_RETRIEVE_MODE, CacheRetrieveMode.BYPASS);
Item fItem = em.find(Item.class, id, paramProperties);
assertEquals(fItem.getItemData(), key);
assertNotCached(Item.class, id);
Object mode = em.getProperties().get(JPAProperties.CACHE_STORE_MODE);
assertEquals(mode, CacheStoreMode.USE);
mode = em.getProperties().get(JPAProperties.CACHE_RETRIEVE_MODE);
assertEquals(mode, CacheRetrieveMode.USE);
}
public void testRefreshWithCacheRetrieveProperty() {
String key = "Test property in refresh.";
String updatedKey = "Updated test property in refresh.";
OpenJPAEntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Item item = new Item();
item.setItemData(key);
em.persist(item);
em.flush();
em.getTransaction().commit();
assertEquals(key, item.getItemData());
int id = item.getItemId();
emf.getCache().evictAll();
assertEquals(key, item.getItemData());
em.setProperty(JPAProperties.CACHE_STORE_MODE, CacheStoreMode.USE);
em.setProperty(JPAProperties.CACHE_RETRIEVE_MODE, CacheRetrieveMode.USE);
Map<String, Object> properties = em.getProperties();
if (!properties.containsKey(JPAProperties.CACHE_STORE_MODE)) {
System.err.println(properties);
fail("Expected " + JPAProperties.CACHE_STORE_MODE + " properties be returned");
}
if (!properties.containsKey(JPAProperties.CACHE_RETRIEVE_MODE)) {
System.err.println(properties);
fail("Expected " + JPAProperties.CACHE_RETRIEVE_MODE + " properties be returned");
}
Map<String, Object> paramProperties = new HashMap<String, Object>();
paramProperties.put(JPAProperties.CACHE_STORE_MODE, CacheStoreMode.BYPASS);
paramProperties.put(JPAProperties.CACHE_RETRIEVE_MODE, CacheRetrieveMode.BYPASS);
Item fItem = em.find(Item.class, id, paramProperties);
assertEquals(key, fItem.getItemData());
assertNotCached(Item.class, id);
fItem.setItemData(updatedKey);
assertEquals(updatedKey, fItem.getItemData());
em.refresh(fItem, paramProperties);
assertEquals(key, fItem.getItemData());
assertNotCached(Item.class, id);
Object mode = em.getProperties().get(JPAProperties.CACHE_STORE_MODE);
assertEquals(mode, CacheStoreMode.USE);
mode = em.getProperties().get(JPAProperties.CACHE_RETRIEVE_MODE);
assertEquals(mode, CacheRetrieveMode.USE);
}
void assertCached(Class<?> cls, Object oid) { void assertCached(Class<?> cls, Object oid) {
assertTrue(cls + ":" + oid + " should be in L2 cache, but not", emf.getCache().contains(cls, oid)); assertTrue(cls + ":" + oid + " should be in L2 cache, but not", emf.getCache().contains(cls, oid));
} }

View File

@ -487,8 +487,8 @@ public class EntityManagerImpl
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T find(Class<T> cls, Object oid, LockModeType mode, Map<String, Object> properties) { public <T> T find(Class<T> cls, Object oid, LockModeType mode, Map<String, Object> properties) {
assertNotCloseInvoked(); assertNotCloseInvoked();
configureCurrentCacheModes(getFetchPlan(), properties); configureCurrentCacheModes(pushFetchPlan(), properties);
configureCurrentFetchPlan(pushFetchPlan(), properties, mode, true); configureCurrentFetchPlan(getFetchPlan(), properties, mode, true);
try { try {
oid = _broker.newObjectId(cls, oid); oid = _broker.newObjectId(cls, oid);
return (T) _broker.find(oid, true, this); return (T) _broker.find(oid, true, this);
@ -765,7 +765,6 @@ public class EntityManagerImpl
Log log = _broker.getConfiguration().getConfigurationLog(); Log log = _broker.getConfiguration().getConfigurationLog();
log.warn(_loc.get("cache-retrieve-override", Exceptions.toString(entity))); log.warn(_loc.get("cache-retrieve-override", Exceptions.toString(entity)));
} }
} }
try { try {
_broker.refresh(entity, this); _broker.refresh(entity, this);
@ -1187,8 +1186,8 @@ public class EntityManagerImpl
assertNotCloseInvoked(); assertNotCloseInvoked();
assertValidAttchedEntity(LOCK, entity); assertValidAttchedEntity(LOCK, entity);
_broker.assertActiveTransaction(); _broker.assertActiveTransaction();
configureCurrentCacheModes(getFetchPlan(), properties); configureCurrentCacheModes(pushFetchPlan(), properties);
configureCurrentFetchPlan(pushFetchPlan(), properties, mode, false); configureCurrentFetchPlan(getFetchPlan(), properties, mode, false);
try { try {
_broker.lock(entity, MixedLockLevelsHelper.toLockLevel(mode), _broker.lock(entity, MixedLockLevelsHelper.toLockLevel(mode),
_broker.getFetchConfiguration().getLockTimeout(), this); _broker.getFetchConfiguration().getLockTimeout(), this);