I am using ehcache (3.8.0) with Java (1.8.0_222) to store values on the heap that are retrieved from a SQL database. The issue I'm having is that the stored values are lists of objects, and those objects also contain lists and over time references in those innermost lists seem to be becoming null. I've tried recreating it in a smaller program without success so I'm guessing it's something daft I've overlooked rather than a problem with ehcache.

The code I'm using for setting up the cache is

final long cacheSize = 1L;
final MemoryUnit cacheUnits = MemoryUnit.GB;

ResourcePoolsBuilder resourcePools = ResourcePoolsBuilder
  .heap(cacheSize, cacheUnits);
CacheConfigurationBuilder<Key, List<MyInterface>> cacheConfigurationBuilder = CacheConfigurationBuilder
  .newCacheConfigurationBuilder(Key.class, (Class<List<MyInterface>>)(Class<?>)List.class, resourcePools)
cacheManager = CacheManagerBuilder
  .withCache(cachesName, cacheConfigurationBuilder)
cache = cacheManager.createCache(cacheName, cacheConfigurationBuilder);

Worked out the problem. Turns out if you use the heap the objects stored aren't immutable. So what was happening was that the program was grabbing an object from the cache, clearing some of its fields when it was done with it, and then when it later grabbed the object those fields were still cleared.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.