Announcement Announcement Module
Collapse
No announcement yet.
Using CGLIB proxy with Ehcache CacheManager Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using CGLIB proxy with Ehcache CacheManager

    I want to use Spring AOP in my spring application. While creating AOP proxy for net.sf.ehcache.CacheManager, spring context initialization fails with the below exception:

    nested exception is org.springframework.aop.framework.AopConfigExcepti on: Could not
    generate CGLIB subclass of class [class net.sf.ehcache.CacheManager]: Common causes of
    this problem include using a final class or a non-visible class; nested exception is
    net.sf.cglib.core.CodeGenerationException: 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.
    The source of the existing CacheManager is: InputStreamConfigurationSource
    [stream=java.io.ByteArrayInputStream@955b34]
    Here's my understanding about this problem - Spring is trying to create AOP proxy for net.sf.ehcache.CacheManager, and it succeeds the first time and gives a default name to the CacheManager __DEFAULT__ (found this by adding debug statements to ehcache code, building it by source and using that in my application). Now if I've multiple cache managers like 'abcCacheManager' and 'xyzCacheManager' (of type EhCacheManagerFactoryBean), Spring encounters multiple net.sf.ehcache.CacheManagers and tries to create proxy objects (something like net.sf.ehcache.CacheManager$$EnhancerByCGLIB$$b18c 5958) for all of them, but with EhCache >=2.5 version, we can't have more than one caches with same name under the same VM.

    I'm using EhCache 2.5.1 and would like to avoid going back to 2.4 just for this purpose. I'm not sure if this is really the problem how I can overcome this problem.

    Note: Note sure if this will help, but I also noticed from the debug statements that CacheManager no-arg constructor is invoked only by the spring/CGLIB proxy generator and xyzCacheManager invokes it by passing configuration as argument.

  • #2
    Is it necessary to create a proxy on the CacheManager class? Usually you need AOP to your own business objects only.

    >> Note: Note sure if this will help, but I also noticed from the debug statements that CacheManager no-arg constructor is invoked...
    This is how CgLib works. It generates a subclass of a class that should be proxied. Then it creates an instance of the subclass. Then it creates an instance of the original class, subclass calls does addition advice logic and delegates to the original instance.

    Comment


    • #3
      Even I was surprised to see that class getting proxied. I'm very new to all this stuff, can we control the classes to be proxied, I think that class is wired as a dependency of another business class and hence the problem. I somehow sense that something is going wrong here.

      Note: I have the below CacheManager wired in one of the dependent business class, I guess that's the reason Spring creates a proxy on that.

      @Resource(name = "searchCacheManager")
      private CacheManager cacheManager;
      Last edited by Swapnil.Sonawane; Sep 10th, 2012, 06:22 AM.

      Comment


      • #4
        @Resource wiring is not a reason to create CgLib proxy.

        So how to configure which classes are to be proxied? It depends on what way do you use to apply Spring AOP.
        If using aop xml namespace or @Aspect classes, restrict pointcut to your packages.
        If using autoproxy mechanism, restrict bean names in BeanNameAutoProxyCreator or create its subclass to override bean name matching method.
        If you are not sure what I am talking about, read the AOP section in the Spring reference. It is better starting point than debugging Spring source code.

        Comment


        • #5
          Can you share application context configurations. From the error message it seems like you have 2 beans having same name i:e CacheManager. Also you mentioned that you have multiple cache managers, normally you have single cache manager for an application, why do you need multiple cache managers ?

          Comment

          Working...
          X