I’ve configured Hibernate in Spring with the following:
jpa:
generate-ddl: true
properties:
jakarta:
missing_cache_strategy: create
persistence:
sharedCache:
mode: ENABLE_SELECTIVE
cache:
provider: org.redisson.jcache.JCachingProvider
hibernate:
generate_statistics: true
ddl-auto: update
cache:
use_second_level_cache: true
use_query_cache: true
region:
factory_class: org.redisson.hibernate.RedissonRegionFactory
redisson:
fallback: true
config: redisson/redisson-dev.yaml
And cacheable entities are annotated as such:
@Entity
@Data
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "entities")
@Cacheable
public class ComponentType {}
However, I am seeing issues putting the entities in cache:
2023-12-24T15:32:43.058Z DEBUG 46344 --- [nio-8080-exec-4] o.hibernate.orm.results.loading.entity : (ENTITYRESULTINITIALIZER) Adding entityInstance to second-level cache: net.shamil.dcc.entities.ComponentType(168864263290750)#0EKRCJTA5ZW3H
2023-12-24T15:32:43.058Z DEBUG 46344 --- [nio-8080-exec-4] o.h.c.s.support.AbstractReadWriteAccess : Caching data from load [region=`entities` (AccessType[read-write])] : key[net.shamil.dcc.entities.ComponentType#0EKRCJTA5ZW3H] -> value[CacheEntry(net.shamil.dcc.entities.ComponentType)]
2023-12-24T15:32:43.059Z DEBUG 46344 --- [nio-8080-exec-4] o.h.c.s.support.AbstractReadWriteAccess : Checking writeability of read-write cache item [timestamp=`6977256641576960`, version=`1`] : txTimestamp=`6977257320656896`, newVersion=`1`
2023-12-24T15:32:43.059Z DEBUG 46344 --- [nio-8080-exec-4] o.h.c.s.support.AbstractReadWriteAccess : Cache put-from-load [region=`AccessType[read-write]` (entities), key=`net.shamil.dcc.entities.ComponentType#0EKRCJTA5ZW3H`, value=`CacheEntry(net.shamil.dcc.entities.ComponentType)`] failed due to being non-writable
20
Looking at the logs, I believe it’s because version=null
:
public abstract class AbstractReadWriteAccess {
class Item {
public boolean isWriteable(long txTimestamp, Object newVersion, Comparator versionComparator) {
if ( log.isDebugEnabled() ) {
log.debugf(
"Checking writeability of read-write cache item [timestamp=`%s`, version=`%s`] : txTimestamp=`%s`, newVersion=`%s`",
timestamp,
version,
txTimestamp,
newVersion
);
}
//noinspection unchecked
return version != null && versionComparator.compare( version, newVersion ) < 0;
}
}
}
My version in the database is 1 and it’s comparing to 1, such that the output of versionComparator.compare( version, newVersion )
is 0
, hence it doesn’t write to cache…