diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java b/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java index 5c462d3de..8ace12ade 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheStoreManager.java @@ -336,9 +336,9 @@ public class DataCacheStoreManager public boolean initialize(OpenJPAStateManager sm, PCState state, FetchConfiguration fetch, Object edata) { boolean rval; DataCache cache = sm.getMetaData().getDataCache(); + boolean updateCache = _ctx.getCacheStoreMode() != DataCacheStoreMode.BYPASS && _ctx.getPopulateDataCache(); if (cache == null || sm.isEmbedded() || _ctx.getCacheRetrieveMode() == DataCacheRetrieveMode.BYPASS || _ctx.getCacheStoreMode() == DataCacheStoreMode.REFRESH) { - // save the return value and return later in case we need to update the cache) rval = super.initialize(sm, state, fetch, edata); } @@ -349,18 +349,17 @@ public class DataCacheStoreManager //### addressed for bug 511 sm.initialize(data.getType(), state); data.load(sm, fetch, edata); - return true; + // no need to update the cache. + updateCache = false; + rval = true; + } else { + // initialize from store manager + rval = super.initialize(sm, state, fetch, edata); } - - // initialize from store manager - if (!super.initialize(sm, state, fetch, edata)) { - return false; - } - rval = true; // same as rval = super.initialize(...) } - // update the cache if configured appropriately. - if (_ctx.getCacheStoreMode() == DataCacheStoreMode.REFRESH && _ctx.getPopulateDataCache()) { + if (cache != null && (rval && updateCache)) { + // update cache if the result came from the database and configured to store or refresh the cache. cacheStateManager(cache, sm); } return rval; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/AbstractCacheModeTestCase.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/AbstractCacheModeTestCase.java index 07c7b7ff6..79c561968 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/AbstractCacheModeTestCase.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/AbstractCacheModeTestCase.java @@ -25,6 +25,7 @@ import javax.persistence.CacheRetrieveMode; import javax.persistence.CacheStoreMode; import javax.persistence.EntityManager; +import org.apache.openjpa.jdbc.meta.ClassMapping; import org.apache.openjpa.lib.jdbc.AbstractJDBCListener; import org.apache.openjpa.lib.jdbc.JDBCEvent; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; @@ -393,4 +394,53 @@ public abstract class AbstractCacheModeTestCase extends AbstractCacheTestCase { entityManagerStoreModeTest(CacheStoreMode.REFRESH, CacheStoreMode.REFRESH, true, true, 1); } } + + public void testResultsFromQueryAreInCache() { + // clear cache + getEntityManagerFactory().getStoreCache().evictAll(); + getEntityManagerFactory().getQueryResultCache().evictAll(); + + EntityManager em = getEntityManagerFactory().createEntityManager(); + String query; + for(Class cls : persistentTypes) { + query = "Select e from " + getAlias(cls) + " e"; + List res = em.createQuery(query).getResultList(); + assertNotNull(String.format("Expected to find some results when running query %s",query), res); + assertTrue(String.format("Expected more than 0 results running query %s",query),res.size() != 0 ) ; + } + for(Class cls : getExpectedInCache()) { + assertCached(getEntityManagerFactory().getCache(), cls, 1, true); + } + + for(Class cls : getExpectedNotInCache()) { + assertCached(getEntityManagerFactory().getCache(), cls, 1, false); + } + em.close(); + } + + public void testResultsFromFindAreInCache() { + // clear cache + getEntityManagerFactory().getStoreCache().evictAll(); + getEntityManagerFactory().getQueryResultCache().evictAll(); + + EntityManager em = getEntityManagerFactory().createEntityManager(); + for(Class cls : persistentTypes) { + assertNotNull(String.format("Expected to find %s::%d from database or from cache", cls, 1),em.find(cls, 1)); + } + for(Class cls : getExpectedInCache()) { + assertCached(getEntityManagerFactory().getCache(), cls, 1, true); + } + + for(Class cls : getExpectedNotInCache()) { + assertCached(getEntityManagerFactory().getCache(), cls, 1, false); + } + em.close(); + } + + private String getAlias(Class cls) { + ClassMapping mapping = + (ClassMapping) getEntityManagerFactory().getConfiguration().getMetaDataRepositoryInstance().getMetaData( + cls, null, true); + return mapping.getTypeAlias(); + } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeEmpty.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeEmpty.java new file mode 100644 index 000000000..d8b73f76f --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeEmpty.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.persistence.cache.jpa; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Cache; + +import org.apache.openjpa.lib.jdbc.JDBCListener; +import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; + +public class TestCacheModeEmpty extends AbstractCacheModeTestCase { + private static OpenJPAEntityManagerFactorySPI emf = null; + private static Cache cache = null; + private static List sql = new ArrayList(); + private static JDBCListener listener; + + private static Class[] expectedInCache = persistentTypes; + private static Class[] expectedNotInCache = {}; + + @Override + public OpenJPAEntityManagerFactorySPI getEntityManagerFactory() { + if (emf == null) { + emf = createEntityManagerFactory("cache-mode-empty"); + assertNotNull(emf); + cache = emf.getCache(); + assertNotNull(cache); + } + return emf; + } + + public JDBCListener getListener() { + if (listener == null) { + listener = new Listener(); + } + return listener; + } + + public List getSql() { + return sql; + } + + public void testCacheables() { + assertCacheables(cache, true); + } + + public void testUncacheables() { + assertUncacheables(cache, true); + } + + public void testUnspecified() { + assertUnspecified(cache, true); + } + + @Override + protected Class[] getExpectedInCache() { + return expectedInCache; + } + + @Override + protected Class[] getExpectedNotInCache() { + return expectedNotInCache; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeUnspecified.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeUnspecified.java new file mode 100644 index 000000000..d1df5e31c --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/cache/jpa/TestCacheModeUnspecified.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.persistence.cache.jpa; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Cache; + +import org.apache.openjpa.lib.jdbc.JDBCListener; +import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; + +public class TestCacheModeUnspecified extends AbstractCacheModeTestCase { + private static OpenJPAEntityManagerFactorySPI emf = null; + private static Cache cache = null; + private static List sql = new ArrayList(); + private static JDBCListener listener; + + private static Class[] expectedInCache = persistentTypes; + private static Class[] expectedNotInCache = {}; + + @Override + public OpenJPAEntityManagerFactorySPI getEntityManagerFactory() { + if (emf == null) { + emf = createEntityManagerFactory("cache-mode-unspecified"); + assertNotNull(emf); + cache = emf.getCache(); + assertNotNull(cache); + } + return emf; + } + + public JDBCListener getListener() { + if (listener == null) { + listener = new Listener(); + } + return listener; + } + + public List getSql() { + return sql; + } + + public void testCacheables() { + assertCacheables(cache, true); + } + + public void testUncacheables() { + assertUncacheables(cache, true); + } + + public void testUnspecified() { + assertUnspecified(cache, true); + } + + @Override + protected Class[] getExpectedInCache() { + return expectedInCache; + } + + @Override + protected Class[] getExpectedNotInCache() { + return expectedNotInCache; + } +} diff --git a/openjpa-persistence-jdbc/src/test/resources/META-INF/caching-persistence.xml b/openjpa-persistence-jdbc/src/test/resources/META-INF/caching-persistence.xml index 23a6990bd..31ed90c09 100644 --- a/openjpa-persistence-jdbc/src/test/resources/META-INF/caching-persistence.xml +++ b/openjpa-persistence-jdbc/src/test/resources/META-INF/caching-persistence.xml @@ -67,4 +67,12 @@ + + META-INF/caching-orm.xml + + + + + +