javajakarta-eespring-datagemfirespring-data-gemfire

Gemfire throws null pointer for @Cachable methods Null result


I have a method which is annotated as @Cacheable and Gemfire is also configured for caching.

The mehtod works fine when returing Not Null values. But if it returns Null then Gemfire throws exception as below :

Caused by: java.lang.NullPointerException: value must not be null
com.gemstone.gemfire.internal.cache.LocalRegion.newUpdateEntryEvent(LocalRegion.java:1744)
at com.gemstone.gemfire.internal.cache.LocalRegion.put(LocalRegion.java:1705)
at com.gemstone.gemfire.internal.cache.AbstractRegion.put(AbstractRegion.java:286)
at org.springframework.data.gemfire.support.GemfireCache.put(GemfireCache.java:68)

Method as below :

    @Override
    @Cachable("date_cache")     
    public String getData(String value){
       return myDataRepository.getLabelByValue(value);
    }

If myDataRepository.getLabelByValue(value) is null then NullPointerException is throw in Gemfire and there is some data retured from database then it works fine.

Please help if there is any config to ignore null value caching in Gemfire.


Solution

  • In reviewing GemFire's source code, there is no way (e.g. GemFire Distributed Configuration System property) to avoid the NPE...

    1744: if (value == null) { 1745: throw new NullPointerException(LocalizedStrings 1746: .LocalRegion_VALUE_MUST_NOT_BE_NULL.toLocalizedString()); 1747: }

    Sorry, your data repository must return a non-null value, unfortunately. This is even true for GemFire 8 (new release).

    In the short term, you can always work around this limitation by extending Spring Data GemFire's CacheManager.getCache() method to return a wrapped version of Cache that discards null values on put(key, value), which is a bit extra work, or wait for JIRA-327.