java - Cache inconsistency - Entity not always persisted in cached Collection -
i'm having issue validation
instance added collection on step
instance. declaration follows:
step class:
@entity @table @cacheable @cache(usage = cacheconcurrencystrategy.read_write) public class step extends abstractentity implements validatablestep { @onetomany(fetch = fetchtype.lazy, cascade = cascadetype.all, orphanremoval = true) @joincolumn(name = "step_id", nullable = false) @cache(usage = cacheconcurrencystrategy.read_write) private set<validation> validations = new hashset<>(); @override public void addvalidation(validation validation) { // stuff ... // add validation instance collection getvalidations().add(validation); } }
validation class:
@entity @table @cacheable @cache(usage = cacheconcurrencystrategy.read_write) @noargsconstructor(access = accesslevel.protected) public class validation extends abstractentity { //some properties }
both classes cacheable
read_write
strategy applied. unidirectional collection of validation
s cached same strategy.
one expect when read-write transaction invokes addvalidation(new validation('username'));
commits, new validation
visible in subsequent read-only transaction. weird thing work , doesn't work...
the first transaction succeeds; see new validation being persisted in database , step
's version property (for optimistic locking puposes) getting incremented. sometimes, 2nd read transaction contains step
instance empty validation
collection...
our hibernate caching config follows:
hibernate.cache.use_second_level_cache = true hibernate.cache.use_query_cache = true hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.singletonehcacheregionfactory hibernate.cache.provider_configuration_file_resource_path = classpath:ehcache.xml net.sf.ehcache.hibernate.cache_lock_timeout = 10000
any idea what's causing weird (and random) behavior?
the hibernate collection cache invalidates existing entries , both entity , collection caches sharing same abstractreadwriteehcacheaccessstrategy, soft-lock acquired when updating data.
because using unidirectional one-to-many association, end validation
table , step_validation link table too. whenever add/remove validation have hit 2 tables , that's less efficient.
i suggest adding @manytoone
side in validation
entity , turn @onetomany
side mapped-by collection:
@onetomany(fetch = fetchtype.lazy, cascade = cascadetype.all, orphanremoval = true, mappedby = "step") @cache(usage = cacheconcurrencystrategy.read_write) private set<validation> validations = new hashset<>();
Comments
Post a Comment