mirror of https://github.com/apache/openjpa.git
OPENJPA-1279:
Adding testcases and fixing cache exclusion logic in DataCacheStoreManager git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@810197 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e6ccbb0c0d
commit
69cf52f68d
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> sql = new ArrayList<String>();
|
||||
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<String> 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;
|
||||
}
|
||||
}
|
|
@ -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<String> sql = new ArrayList<String>();
|
||||
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<String> 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;
|
||||
}
|
||||
}
|
|
@ -67,4 +67,12 @@
|
|||
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
<persistence-unit name="cache-mode-empty">
|
||||
<mapping-file>META-INF/caching-orm.xml</mapping-file>
|
||||
<properties>
|
||||
<!-- Connection info is passed in via system properties -->
|
||||
<!-- Cache configuration ie openjpa.DataCache is passed in at EMF initialization -->
|
||||
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
|
|
Loading…
Reference in New Issue