5

I am trying to get EhCache configured to handle authorization caching in my Apache Shiro enabled web service. Currently I am getting the following exception:

org.apache.shiro.cache.CacheException: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:

  1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
  2. Shutdown the earlier cacheManager before creating new one with same name.

My shiro.ini looks like:

[main]   
...    
cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager 
cacheManager.cacheManagerConfigFile = classpath:ehcache.xml  
securityManager.cacheManager = $cacheManager 

From this StackOverflow post it looks like people using Spring have gotten around this issue by forcing the CacheManager to be a singleton: Another unnamed CacheManager already exists in the same VM (ehCache 2.5).

Is anybody aware of work-arounds not using Spring initialization (I'm working within the dropwizard framework and have no need to pull in Spring)? Is there some manner of enforcing singleton configuration from the shiro.ini?

Thank you in advance!

Leos Literak
  • 8,805
  • 19
  • 81
  • 156
Whitney Zoller
  • 133
  • 2
  • 7

1 Answers1

3

Create a custom class that extends EhCacheManager and set your cacheManager in the constructor. This (net.sf.ehcache.CacheManager.create()) allows you to reuse an already existing cachemanager.

package lekkie.omotayo

public class MyShiroCacheManager extends EhCacheManager
{

    public CacheManager()
    {
        setCacheManager(net.sf.ehcache.CacheManager.create());
    }
}

Then you can do this:

cacheManager = lekkie.omotayo.MyShiroCacheManager
cacheManager.cacheManagerConfigFile = classpath:ehcache.xml
securityManager.cacheManager = $cacheManager
Lekkie
  • 355
  • 5
  • 18