javaspringhibernateehcacheehcache-2

Hibernate 5.4.25 second level cache and extending the SingletonEhcacheRegionFactory class


In Hibernate 5.2.18, it was possible to set the hibernate configuration option hibernate.cache.region.factory_class to a class value. This allowed me to extend the class org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory and implement required extra code for my clustered application. This implemented class could then be referenced as the config value for the hibernate.cache.region.factory_class config in the hibernate settings.

Example in spring-config.xml:

<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">com.example.MySingletonEhCacheRegionFactory</prop>

Implemented class:

public class MySingletonEhCacheRegionFactory extends
        SingletonEhCacheRegionFactory {
// Implementation
}

I am now trying to upgrade to version 5.4.25, however in version 5.3.0 the SingletonEhCacheRegionFactory was moved to an internal class and the code was altered.

On top of this, the hibernate.cache.region.factory_class config now expects the shortened name for the class. This is stated in the Hibernate 5.4 documentation.

Example:

<prop key="hibernate.cache.region.factory_class">ehcache-singleton</prop>       

So my questions are:

Firstly, is it still possible to extend the SingletonEhcacheRegionFactory class from the new location org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory and add my custom implementation as a shortened value to the hibernate configuration?

Or should I be extending the net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory class instead? It used to be recommended to use the other class over this (see here) but it says nothing about version 5+.

For Hibernate 4, use org.hibernate.cache.ehcache.EhCacheRegionFactory instead of net.sf.ehcache.hibernate.EhCacheRegionFactory.

Secondly, can we reference our own class in the hibernate config using the shortened name?

<prop key="hibernate.cache.region.factory_class">myEhcache-singleton</prop>     

Any insight into the above is appreciated!


Solution

  • I think you should extent the org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory class, but the new one is slightly different from the org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory so you will need to rewrite your current code.

    If you try to extent net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory you will face some problems like SingletonEhCacheRegionFactory is no longer available when upgrading from Hibernate 5.2 to 5.3

    About how to reference it, it is implementing org.hibernate.boot.registry.selector.StrategyRegistrationProvider interface, like:

    package my.package.MyStrategyRegistrationProviderImpl;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.hibernate.boot.registry.selector.SimpleStrategyRegistrationImpl;
    import org.hibernate.boot.registry.selector.StrategyRegistration;
    import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
    import org.hibernate.cache.spi.RegionFactory;
    
    /**
     * Makes the 1 contained region factory implementations available to the Hibernate
     * {@link org.hibernate.boot.registry.selector.spi.StrategySelector} service.
     */
    public class MyStrategyRegistrationProviderImpl implements StrategyRegistrationProvider {
    
        @Override
        @SuppressWarnings("unchecked")
        public Iterable<StrategyRegistration> getStrategyRegistrations() {
            final List<StrategyRegistration> strategyRegistrations = new ArrayList<StrategyRegistration>( 1 );
    
            strategyRegistrations.add(
                    new SimpleStrategyRegistrationImpl(
                            RegionFactory.class,
                            CustomSingletonEhCacheRegionFactory.class,
                            "custom-ehcache-singleton",
                            CustomSingletonEhCacheRegionFactory.class.getName(),
                            CustomSingletonEhCacheRegionFactory.class.getSimpleName(),
                            // legacy impl class name
                            "org.hibernate.cache.SingletonEhCacheRegionFactory",
                            "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"
                    )
            );
    
            return strategyRegistrations;
        }
    }
    

    and creating a file called: META-INF/services/org.hibernate.boot.registry.selector.StrategyRegistrationProvider with the full class name:

    #
    # Hibernate, Relational Persistence for Idiomatic Java
    #
    # License: GNU Lesser General Public License (LGPL), version 2.1 or later
    # See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
    #
    #
    # Hibernate, Relational Persistence for Idiomatic Java
    #
    # License: GNU Lesser General Public License (LGPL), version 2.1 or later
    # See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
    
    my.package.MyStrategyRegistrationProviderImpl