0

At application startup, I attempt to create a cache like this:

The code that I use to create the cache is as follows:

 final Configuration entityConfiguration = myCacheManager.getCacheConfiguration( "entity" );
 Objects.requireNonNull( entityConfiguration );
 myCacheManager.createCache( "XXXX", entityConfiguration );

This results in the following exception:

org.infinispan.commons.CacheConfigurationException: ISPN000373: Attempted to start a cache using configuration template 'XXXX'
    at org.infinispan.manager.DefaultCacheManager.wireAndStartCache(DefaultCacheManager.java:616)
    at org.infinispan.manager.DefaultCacheManager.createCache(DefaultCacheManager.java:589)
    at org.infinispan.manager.DefaultCacheManager.internalGetCache(DefaultCacheManager.java:475)
    at org.infinispan.manager.DefaultCacheManager.getCache(DefaultCacheManager.java:461)
    at org.infinispan.manager.DefaultCacheManager.getCache(DefaultCacheManager.java:447)
    at org.infinispan.manager.DefaultCacheManager.createCache(DefaultCacheManager.java:413)

I understand this exception to mean that I can not have a cache named the same as a cache template.

I looked into org.infinispan.manager.DefaultCacheManager (version 9.2.5.Final) and found this implementation:

    @Override
    public <K, V> Cache<K, V> createCache(String name, Configuration configuration) {
      defineConfiguration(name, configuration);
      return getCache(name);
    }

This defines a configuration with the name passed in. In this case "XXXX", and then goes on to attempt to create a cache with the exact same name.

In the call to getCache(name) it calls through to wireAndStartCache which checks if the configuration is the same as the cache name. It always is, because the previous code passed the exact same string. I don't understand how this method could ever possibly work.

How can I avoid this error and create a cache?

WW.
  • 23,793
  • 13
  • 94
  • 121

1 Answers1

1

The problem with the code I posted above is that getCacheConfiguration( "entity" ); returns a template. So you can get it to work by using a builder to effectively clone it but without the template flag set.

final Configuration entityTemplate = myCacheManager.getCacheConfiguration( "entity" );
Objects.requireNonNull( entityTemplate  );
final ConfigurationBuilder builder = new ConfigurationBuilder();
builder.read( entityTemplate );
builder.template( false );
myCacheManager.createCache( "XXXX", builder.build() );
WW.
  • 23,793
  • 13
  • 94
  • 121