Hibernate with Ehache failing to init SessionFactory

@Vlad, i am not sure if this is the right forum to post this however you can guide if you have an idea

I did a PoC of Hibernate with Ehcache in my local eclipse environment with two different java processes running in two different eclipse instances and I could successfully get it working. Now, when I deploy the same in different java processes of actual product, it is failing to create hibernate session factory with following error: any help in this regard is appreciated…

Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]

org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]

        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:261)

        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:225)

        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)

        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:273)

        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)

        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)

        at com.avaya.pim.jdbc.hibernate.HibernateUtil.initializeSessionFactory(HibernateUtil.java:137)

        at com.avaya.pim.pam.PAM.main(PAM.java:208)

Caused by: org.hibernate.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 the 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: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]

Below are the versions of Ehcache and Hibernate I am using:

  1. What version of Ehcache you are currently using; 2.10.5 version of ehcache
  2. Paste the configuration for the Cache/CacheManager you have an issue with; pasted below in description
  3. Add any name and version of other library or framework you use Ehcache with (e.g. Hibernate); Hibernate 4.3.11
  4. Providing JDK and OS versions may be useful as well.: jdk 1.7, RHEL

ehcache.xml configuration:

<ehcache xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=“ehcache.xsd” updateCheck=“true” monitoring=“autodetect” dynamicConfig=“true”>

<cache

name=“com.avaya.pim.jdbc.hibernate.PimAttribute”

maxElementsInMemory=“500”

eternal=“false”

timeToIdleSeconds=“120000”

timeToLiveSeconds=“120000”

overflowToDisk=“false”

diskPersistent=“false”

memoryStoreEvictionPolicy=“LRU”>

</cache>

</ehcache>

hibernate.cfg.xml relevent configuration:

<property name=“hibernate.cache.use_second_level_cache”>true</property>

<!-- <property name=“hibernate.cache.provider_class”>org.hibernate.cache.NoCacheProvider</property> -->

<property name=“hibernate.cache.region.factory_class”>org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

Use the SingletonEhCacheRegionFactory instead of EhCacheRegionFactory:

<property name=“hibernate.cache.region.factory_class”>org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</property>

Team, I have a quick query regarding this, since i am hitting the same issue with Hibernate 5.6.11

In the version 5.6.11 jars for hibernate-ehcache-5.6.11, i am seeing that SingletonEhCacheRegionFactory has been moved to the internal folder, Does that mean now we have to use org.hibernate.cache.ehcache.internal.SingletonEhCacheRegionFactory as the value?

Thank You in Advance!!

How about using the short name ehcache-singleton? Also see the documentation for details.

Somehow when using that name, i am getting the following exception

Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [ehcache-singleton] as strategy [org.hibernate.cache.spi.RegionFactory]
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:156)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:239)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:192)
at org.hibernate.cache.internal.RegionFactoryInitiator.resolveRegionFactory(RegionFactoryInitiator.java:93)
at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:47)
at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:32)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
… 54 more
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [ehcache-singleton]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:133)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:152)
… 61 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : ehcache-singleton
at org.hibernate.boot.registry.classloading.internal.AggregatedClassLoader.findClass(AggregatedClassLoader.java:210)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:467)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:130)
… 62 more

Any thoughts reasoning will be highly appreciated.

That looks like a bug. Please create a JIRA issue in the issue tracker: https://hibernate.atlassian.net

Team,
I have a quick question on whether the change for SingletonEhcacheRegionFactory class name change from version 5.2.10 to 5.6.12 is an intended one.

I can see the class name has been changed from SingletonEhCacheRegionFactory to
SingletonEhcacheRegionFactory. Note the C in Cache changed to small case c as shown in the image below.

I wanted to query if this has been an intended change, the reason being we do use modularized version of jars where-in we do bndfication and then use the jars. Due to this change we were getting exception as we were using the other name of the class.

Please let me know about if this is an intended change.

Thanks in Advance!!

Appreciate your help.

Well, the name of the technology actually doesn’t have a capital “C” as you can see on their website, so I assume this was intentional. But either way, this module was maintained by the Ehcache developers, so better ask them.

@beikov : Sure Thanks for the update. For this module do they maintain repository somewhere? I checked ehcache · GitHub but didn’t find hibernate related repository to query for this information.

The module was maintained as part of the hibernate-orm repository, but was removed in version 6 in favor of the jcache module, which was also developed by the Ehcache developers.

Hi team , in my project we upgraded java 17 and spring 3.0.7 and hiberante 6.x version previousaly we are using ehcacheRegion factory but after started to hibernate 6.x we moved to jcache properties.setProperty(“hibernate.cache.use_second_level_cache”,“true”);
properties.setProperty(“hibernate.cache.region.factory_class”, “org.hibernate.cache.jcache.JCacheRegionFactory”);
properties.setProperty(“hibernate.javax.cache.provider”,“org.ehcache.jsr107.EhcacheCachingProvider”);
// properties.setProperty(“hibernate.javax.cache.provider”,“file:/META-INF/disaster-relief-ehcache.xml”);
properties.setProperty(“hibernate.cache.ehcache.missing_cache_strategy”,“create”);
properties.setProperty(“org.ehcache.ConfigurationResourceName”, disasterReliefConfigurationResourceName);
properties.setProperty(“org.ehcache.ManagerName”, disasterReliefManagerName);
properties.setProperty(“jakarta.persistence.sharedCache.mode”, Mode

now I am getting error Unable to build Hibernate SessionFactory; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.CacheImplementor could you please help is me

Create a new topic on Discourse for your question and post the full stack trace there.

thank you for your replay but where I can create new topic