Menu

ehcache in an Enterprise Application env?

2005-08-07
2013-05-14
  • Bruce W. Hoylman

    Ciao!

    If there is more than one application deployed in an EAR file to a server, will ehcache work at all?  Is there a way to point each application to its own ehcache CacheManager singleton?

    The way I believe I understand an enterprise application deployment is that each application enjoys its own classloader, each of which is a child of a common parent loader.  The application classloaders can see up, but cannot see each other.  If this is the case, then I can give each its own cache instance, but I would need to be able to point each to a different diskStore location.  As this is hardcoded in ehcache.xml. then I would need a unique ehcache.xml for each application that gets deployed from the ear, right?

    Man, this is killing me ... (c:

    If anyone has any experience, comments, suggestions on this, or if I should simply abandon now, I'd sure appreciate some input.

    Peace.

     
    • Josh

      Josh - 2005-08-08

      Hi Bruce,

      I talked a little about this in my singleton post but here is the deal: if you package up the ehcache.jar as a utility jar in the ear, then all of your war files will see the same instance of the singleton CacheManager.  Suppose you have 2 wars and each has its own ehcache.xml.  Then the CacheManager will be loaded using the ehcache.xml that it finds first (the one from the war that initially calls it).  This is because the was classloader will look for the CacheManager and not find it so it will delegate to the ear classloader which will find it in the utility jar.  Both war classloaders will delegate to the same ear classloader hence both wars will use the same CacheManager.  The solution, is to put a copy of ehcache.jar inside the WEB-INF/lib of both or your wars.  This way, the war classloader will find its own CacheManager and will use its own ehcache.xml.  Note some app servers let you change the classloader settings so things work a little differently but what I described should work with your default settings.  I am most familiar with WebSphere so if you are using that I can explain a little more on those options if you can't get it to work.  Let me know if this helps.

      Josh

       
    • Greg Luck

      Greg Luck - 2005-10-03

      I saw user497 gave a workaround for this.

      CacheManager can now be constructed using new CacheManager(...) which does not create a singleton. The existing factory methods which do create singletons remain. The two coexist peacefully.

      A check has been added to make sure that multiple CacheManagers do not share the same diskPath.

      You would specify a file path, URL or InputStream for each ehcache.xml config. There are examples in CacheManagerTest.

      If this does not help let me know.

       

Log in to post a comment.

MongoDB Logo MongoDB