• New Feature
  • Status: Closed
  • 2 Major
  • Resolution: Fixed
  • ehcache-core
  • alexsnaps
  • Reporter: jkronegg
  • October 06, 2011
  • 0
  • Watchers: 4
  • July 27, 2012
  • December 05, 2011

Description

h3. Introduction This is a feature request for an alternative to configure the [{{CacheManager}}|http://ehcache.org/xref/net/sf/ehcache/CacheManager.html] name.

h3. The problem The {{CacheManager}} name is currently defined in the {{ehcache.xml}} file :

<ehcache name="myCacheManagerName"> ...

When the {{ehcache.xml}} file is referenced from a {{persistence.xml}}’s persistence-unit (e.g. with Hibernate), the {{ehcache.xml}} URL is given as a property. When having more than one persistence-unit (e.g. one for each company branch), it is required to have as much {{ehcache.xml}} as persistence-units in order to have a {{CacheManager}} name associated with the persistence-unit. This is important because some third-party libraries use the {{CacheManager}} name as a key (e.g. when replication using JGroups, the JGroups group name is the {{CacheManager}} name, which leads to unpredictable results since all cache replication messages will be merged in a singleton JGroups group instead of having one JGroups group per persistence-unit).

For example:

<persistence-unit name="PU1">
  ...
  <properties>
    <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
    ...
    <property name="net.sf.ehcache.configurationResourceName" value="ehcache-PU1.xml"/>
  </properties>
</persistence-unit>

<persistence-unit name="PU2">
  ...
  <properties>
    <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
    ...
    <property name="net.sf.ehcache.configurationResourceName" value="ehcache-PU2.xml"/>
  </properties>
</persistence-unit>

<persistence-unit name="PU3">
  ...
  <properties>
    <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
    ...
    <property name="net.sf.ehcache.configurationResourceName" value="ehcache-PU3.xml"/>
  </properties>
</persistence-unit>
<ehcache name="myCacheManagerNamePU1"> ...
<ehcache name="myCacheManagerNamePU2"> ...
<ehcache name="myCacheManagerNamePU3"> ...

It would be convenient to have a single ehcache.xml (e.g. because each company branch must have the same cache *configuration*) and to allows for choosing the {{CacheManager}} name from outside the {{ehcache.xml}}.

h3. Feature requested Allows for giving the {{CacheManager}} a name by using a configuration property. This configuration property would override the {{CacheManager}} name set in the {{ehcache.xml}}.

The {{persistence.xml}} file would thus looks like:

<persistence-unit name="PU1">
  ...
  <properties>
    <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
    ...
    <property name="net.sf.ehcache.configurationResourceName" value="ehcache.xml"/>
    <property name="net.sf.ehcache.cacheManagerName" value="myCacheManagerNamePU1"/>
  </properties>
</persistence-unit>

<persistence-unit name="PU2">
  ...
  <properties>
    <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
    ...
    <property name="net.sf.ehcache.configurationResourceName" value="ehcache.xml"/>
    <property name="net.sf.ehcache.cacheManagerName" value="myCacheManagerNamePU2"/>
  </properties>
</persistence-unit>


<persistence-unit name="PU3">
  ...
  <properties>
    <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
    ...
    <property name="net.sf.ehcache.configurationResourceName" value="ehcache.xml"/>
    <property name="net.sf.ehcache.cacheManagerName" value="myCacheManagerNamePU3"/>
  </properties>
</persistence-unit>
<ehcache name="myCacheManagerDefaultName"> ...

h3. Implementation idea In order to implement this feature, the [{{net.sf.ehcache.hibernate.EhCacheRegionFactory}}|http://ehcache.org/xref/net/sf/ehcache/hibernate/EhCacheRegionFactory.html] would be modified as such:

  • in the start method, read the {{net.sf.ehcache.cacheManagerName}} property from the {{Properties}} given in parameters
  • in the start method, if the property above was set, modify the previously parsed/loaded [{{Configuration}} http://ehcache.org/xref/net/sf/ehcache/config/Configuration.html] by calling its {{setName()}} method in order to change the {{Configuration}}’s {{CacheManager}} name.

Of course, the same changes would be reflected to the other factories:

  • [{{net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory}} http://ehcache.org/xref/net/sf/ehcache/hibernate/SingletonEhCacheRegionFactory.html]
  • [{{net.sf.ehcache.hibernate.EhCacheProvider}} http://ehcache.org/xref/net/sf/ehcache/hibernate/EhCacheProvider.html] (not sure this is required since the class is deprecated)
  • [{{net.sf.ehcache.hibernate.SingletonEhCacheProvider}} http://ehcache.org/xref/net/sf/ehcache/hibernate/SingletonEhCacheProvider.html] (not sure this is required since the class is deprecated)

The documentation must also be updated to reflect this new way of setting the {{CacheManager}} name.

Comments

Fiona OShea 2011-10-11

Alex what is your opinion on this feature request?

Alexander Snaps 2011-10-24

Even though people should probably have a ehcache.xml where they should put the CacheManager’s name, we should do this. The rational is that we try to have people move away from the complex ehcache.xml tuning. Hibernate already let’s someone do w/o a ehcache.xml. So this seems something we should do. Though we’re code frozen yet… Should I proceed with this nonetheless ? Pretty isolated change in the Hibernate code we ship as part of ehcache-core. I’ll also need to provide the Hibernate team with the fix.

Alexander Snaps 2011-12-05

r5026 on branches/ehcache-core-2.5.x Added the net.sf.ehcache.cacheManagerName prop