From 0e5859716b54e57c3c6f86726a613b3a3f699456 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Fri, 13 May 2016 12:52:27 +1200 Subject: [PATCH] #702 - IllegalStateException: No bean cache enabled error ... when updating bean that is not using caching (but is related to a bean with @Cache) --- .../server/core/PersistRequestBean.java | 2 +- .../server/deploy/BeanDescriptor.java | 5 +- .../deploy/BeanDescriptorCacheHelp.java | 70 ++++++++++++++----- .../server/deploy/BeanPropertyAssocOne.java | 17 +++-- .../avaje/tests/cache/TestCacheCustomer.java | 20 ++++++ 5 files changed, 89 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/avaje/ebeaninternal/server/core/PersistRequestBean.java b/src/main/java/com/avaje/ebeaninternal/server/core/PersistRequestBean.java index eb57cef1b..dbe649870 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/core/PersistRequestBean.java +++ b/src/main/java/com/avaje/ebeaninternal/server/core/PersistRequestBean.java @@ -305,7 +305,7 @@ public final class PersistRequestBean extends PersistRequest implements BeanP * Set the cache notify status. */ private void setNotifyCache() { - this.notifyCache = beanDescriptor.isCacheNotify(publish); + this.notifyCache = beanDescriptor.isCacheNotify(type, publish); } /** diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptor.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptor.java index dbf5f99f9..fd112a940 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptor.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptor.java @@ -726,6 +726,7 @@ public class BeanDescriptor implements MetaBeanInfo, BeanType { } docMapping = docStoreAdapter.createDocMapping(); docStoreAdapter.registerPaths(); + cacheHelp.deriveNotifyFlags(); } public void initInheritInfo() { @@ -1059,12 +1060,12 @@ public class BeanDescriptor implements MetaBeanInfo, BeanType { /** * Return true if the persist request needs to notify the cache. */ - public boolean isCacheNotify(boolean publish) { + public boolean isCacheNotify(PersistRequest.Type type, boolean publish) { if (draftable && !publish) { // no caching when editing draft beans return false; } - return cacheHelp.isCacheNotify(); + return cacheHelp.isCacheNotify(type); } /** diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptorCacheHelp.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptorCacheHelp.java index cd71f3fd0..310a9bb20 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptorCacheHelp.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptorCacheHelp.java @@ -14,6 +14,7 @@ import com.avaje.ebeaninternal.server.cache.CachedBeanDataFromBean; import com.avaje.ebeaninternal.server.cache.CachedBeanDataToBean; import com.avaje.ebeaninternal.server.cache.CachedManyIds; import com.avaje.ebeaninternal.server.core.CacheOptions; +import com.avaje.ebeaninternal.server.core.PersistRequest; import com.avaje.ebeaninternal.server.core.PersistRequestBean; import com.avaje.ebeaninternal.server.querydefn.NaturalKeyBindParam; import com.avaje.ebeaninternal.server.transaction.DefaultPersistenceContext; @@ -33,6 +34,8 @@ import java.util.Map; */ final class BeanDescriptorCacheHelp { + private static final Logger logger = LoggerFactory.getLogger(BeanDescriptorCacheHelp.class); + private static final Logger queryLog = LoggerFactory.getLogger("org.avaje.ebean.cache.QUERY"); private static final Logger beanLog = LoggerFactory.getLogger("org.avaje.ebean.cache.BEAN"); private static final Logger manyLog = LoggerFactory.getLogger("org.avaje.ebean.cache.COLL"); @@ -60,6 +63,16 @@ final class BeanDescriptorCacheHelp { private final ServerCache naturalKeyCache; private final ServerCache queryCache; + /** + * Set to true if all persist changes need to notify the cache. + */ + private boolean cacheNotifyOnAll; + + /** + * Set to true if delete changes need to notify cache. + */ + private boolean cacheNotifyOnDelete; + BeanDescriptorCacheHelp(BeanDescriptor desc, ServerCacheManager cacheManager, CacheOptions cacheOptions, boolean cacheSharableBeans, BeanPropertyAssocOne[] propertiesOneImported) { @@ -91,6 +104,43 @@ final class BeanDescriptorCacheHelp { } } + /** + * Derive the cache notify flags. + */ + public void deriveNotifyFlags() { + cacheNotifyOnAll = (beanCache != null || queryCache != null); + cacheNotifyOnDelete = !cacheNotifyOnAll && isNotifyOnDeletes(); + + if (logger.isDebugEnabled()) { + if (isBeanCaching() || isQueryCaching() || cacheNotifyOnAll || cacheNotifyOnDelete) { + String notifyMode = cacheNotifyOnAll ? "All" : (cacheNotifyOnDelete ? "Delete" : "None"); + logger.debug("l2 caching on {} - beanCaching:{} queryCaching:{} notifyMode:{} ", + desc.getFullName(), isBeanCaching(), isQueryCaching(), notifyMode); + } + } + } + + /** + * Return true if there is an imported bi-directional relationship to a bea + * that does have bean caching enabled. + */ + private boolean isNotifyOnDeletes() { + for (int i = 0; i < propertiesOneImported.length; i++) { + if (propertiesOneImported[i].isCacheNotify()) { + return true; + } + } + return false; + } + + /** + * Return true if the persist request needs to notify the cache. + */ + boolean isCacheNotify(PersistRequest.Type type) { + return cacheNotifyOnAll + || cacheNotifyOnDelete && (type == PersistRequest.Type.DELETE || type == PersistRequest.Type.DELETE_PERMANENT); + } + /** * Return true if there is currently query caching for this type of bean. */ @@ -105,22 +155,6 @@ final class BeanDescriptorCacheHelp { return beanCache != null; } - /** - * Return true if the persist request needs to notify the cache. - */ - boolean isCacheNotify() { - - if (isBeanCaching() || isQueryCaching()) { - return true; - } - for (int i = 0; i < propertiesOneImported.length; i++) { - if (propertiesOneImported[i].getTargetDescriptor().isBeanCaching()) { - return true; - } - } - return false; - } - CacheOptions getCacheOptions() { return cacheOptions; } @@ -539,7 +573,7 @@ final class BeanDescriptorCacheHelp { } /** - * Add appropriate cache changes to support delete. + * Add appropriate cache changes to support delete by id. */ void handleDelete(Object id, CacheChangeSet changeSet) { if (beanCache != null) { @@ -549,7 +583,7 @@ final class BeanDescriptorCacheHelp { } /** - * Add appropriate cache changes to support delete. + * Add appropriate cache changes to support delete bean. */ void handleDelete(Object id, PersistRequestBean deleteRequest, CacheChangeSet changeSet) { queryCacheClear(changeSet); diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocOne.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocOne.java index d79ddef60..dd503959c 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocOne.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocOne.java @@ -134,19 +134,28 @@ public class BeanPropertyAssocOne extends BeanPropertyAssoc { this.relationshipProperty = relationshipProperty; } - BeanPropertyAssocMany getRelationshipProperty() { - return relationshipProperty; + /** + * Return true if this relationship needs to maintain/update L2 cache. + */ + boolean isCacheNotify() { + return targetDescriptor.isBeanCaching() && relationshipProperty != null; } + /** + * Clear the L2 relationship cache for this property. + */ void cacheClear() { - if (targetDescriptor.isBeanCaching() && relationshipProperty != null) { + if (isCacheNotify()) { targetDescriptor.cacheManyPropClear(relationshipProperty.getName()); } } + /** + * Clear part of the L2 relationship cache for this property. + */ void cacheDelete(boolean clear, EntityBean bean, CacheChangeSet changeSet) { - if (targetDescriptor.isBeanCaching() && relationshipProperty != null) { + if (isCacheNotify()) { if (clear) { changeSet.addManyClear(targetDescriptor, relationshipProperty.getName()); } else { diff --git a/src/test/java/com/avaje/tests/cache/TestCacheCustomer.java b/src/test/java/com/avaje/tests/cache/TestCacheCustomer.java index 39c776f7d..5682b8353 100644 --- a/src/test/java/com/avaje/tests/cache/TestCacheCustomer.java +++ b/src/test/java/com/avaje/tests/cache/TestCacheCustomer.java @@ -2,6 +2,7 @@ package com.avaje.tests.cache; import java.util.List; +import com.avaje.tests.model.basic.Country; import org.junit.Assert; import org.junit.Test; @@ -14,6 +15,25 @@ import com.avaje.tests.model.basic.ResetBasicData; public class TestCacheCustomer extends BaseTestCase { + @Test + public void testUpdateAddress() { + + ResetBasicData.reset(); + + Country nz = Ebean.getReference(Country.class, "NZ"); + Address address = new Address(); + address.setLine1("Some Place"); + address.setCity("Auckland"); + address.setCountry(nz); + + Ebean.save(address); + + address.setLine2("Else"); + Ebean.save(address); + + Ebean.delete(address); + } + @Test public void test() {