[infinispan-issues] [JBoss JIRA] (ISPN-2609) Infinispan SpringCache throws java.lang.NullPointerException: Null values are not supported!

Roland Csupor (JIRA) jira-events at lists.jboss.org
Thu Jan 10 11:03:08 EST 2013


    [ https://issues.jboss.org/browse/ISPN-2609?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12744903#comment-12744903 ] 

Roland Csupor commented on ISPN-2609:
-------------------------------------

No, it doesn't mean this. I have a method with a org.springframework.cache.annotation.Cacheable annotation, which sometimes returns null. In this case I got a NullPointerException. I used ehcache before, and Null return value is supported by this. I switched to infinispan some weaks ago, and I got exceptions.

I attach my solution below:


I created a cache implementation which handles null values and forwards the wrapped null and the values to the original, infinispan cache implementation:

{noformat}
import java.io.Serializable;

import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;

public class NullValueSupportCache implements Cache {

    private static final Object NULL_VALUE_HOLDER = new NullHolder();

    private final Cache nativeCache;

    public NullValueSupportCache(final Cache nativeCache) {
        this.nativeCache = nativeCache;
    }

    private static class NullHolder implements Serializable {
        private static final long serialVersionUID = 7697658481336079443L;
    }

    @Override
    public String getName() {
        return this.nativeCache.getName();
    }

    @Override
    public Object getNativeCache() {
        return this.nativeCache.getNativeCache();
    }

    @Override
    public ValueWrapper get(final Object key) {
        final ValueWrapper valueWrapper = this.nativeCache.get(key);

        if (valueWrapper == null) {
            return null;
        }

        final Object value = valueWrapper.get();
        if (value instanceof NullHolder) {
            return new SimpleValueWrapper(null);
        } else {
            return valueWrapper;
        }
    }

    @Override
    public void put(final Object key, final Object value) {
        if (value == null) {
            this.nativeCache.put(key, NULL_VALUE_HOLDER);
        } else {
            this.nativeCache.put(key, value);
        }
    }

    @Override
    public void evict(final Object key) {
        this.nativeCache.evict(key);
    }

    @Override
    public void clear() {
        this.nativeCache.clear();
    }

}
{noformat}

And I have a CacheManager to use this implementation:

{noformat}
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

public class NullValueSupportSpringCacheManager implements CacheManager {

    private final CacheManager nativeCacheManager;

    private final Collection<String> cacheNames = new HashSet<String>();

    public NullValueSupportSpringCacheManager(final CacheManager nativeCacheManager) {
        this.nativeCacheManager = nativeCacheManager;
    }

    @Override
    public Cache getCache(final String name) {
        this.cacheNames.add(name);
        return new NullValueSupportCache(this.nativeCacheManager.getCache(name));
    }

    @Override
    public Collection<String> getCacheNames() {
        return Collections.unmodifiableCollection(this.cacheNames);
    }

}
{noformat}
                
> Infinispan SpringCache throws java.lang.NullPointerException: Null values are not supported!
> --------------------------------------------------------------------------------------------
>
>                 Key: ISPN-2609
>                 URL: https://issues.jboss.org/browse/ISPN-2609
>             Project: Infinispan
>          Issue Type: Bug
>          Components: Spring integration
>    Affects Versions: 5.1.6.FINAL
>            Reporter: Roland Csupor
>            Assignee: Mircea Markus
>
> I trying to use Infinispan as Spring cache, but if my function returns null, I got an exception, cause Spring tries to cache this result value:
> {noformat}
> Caused by: java.lang.NullPointerException: Null values are not supported!
>         at org.infinispan.CacheImpl.assertKeyValueNotNull(CacheImpl.java:203) ~[infinispan-core-5.1.6.FINAL.jar:5.1.6.FINAL]
>         at org.infinispan.CacheImpl.put(CacheImpl.java:699) ~[infinispan-core-5.1.6.FINAL.jar:5.1.6.FINAL]
>         at org.infinispan.CacheImpl.put(CacheImpl.java:694) ~[infinispan-core-5.1.6.FINAL.jar:5.1.6.FINAL]
>         at org.infinispan.CacheSupport.put(CacheSupport.java:53) ~[infinispan-core-5.1.6.FINAL.jar:5.1.6.FINAL]
>         at org.infinispan.spring.provider.SpringCache.put(SpringCache.java:83) ~[infinispan-spring-5.1.6.FINAL.jar:5.1.6.FINAL]
>         at org.springframework.cache.interceptor.CacheAspectSupport.update(CacheAspectSupport.java:390) ~[spring-context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
>         at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:218) ~[spring-context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
>         at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66) ~[spring-context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
> {noformat}
> Did I misconfigured something?

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira


More information about the infinispan-issues mailing list