hazelcast/hazelcast-hibernate

Hibernate 5.3.* Support with jcache fails on getCachingProviders because of not a subtype of javax.cache.spi.CachingProvider

supralog-ecc opened this issue · 5 comments

Description :
Recently, we have upgraded the version of Hibernate from 4.3.5 to 5.3.7, then we can't launch our project right now in active mode of second level cache with Hazelcast implementation.

Working Environments :
Java 8, Glassfih 4.1, JPA 2.2, Hibernate-* 5.3.7, Hazelcast 3.12, cache-api 1.1.0

Following is a snippet of our related configuration :
<property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.jcache.JCacheRegionFactory" /> <property name="hibernate.cache.jcache.config" value="classpath:myhazelcast.xml"/> <property name="hibernate.javax.cache.provider" value="com.hazelcast.cache.HazelcastCachingProvider" />

Here is the error message we meet :

java.util.ServiceConfigurationError: javax.cache.spi.CachingProvider: Provider com.hazelcast.cache.HazelcastCachingProvider not a subtype
at java.util.ServiceLoader.fail(ServiceLoader.java:239)
at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:376)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
at javax.cache.Caching$CachingProviderRegistry$1.run(Caching.java:448)
at javax.cache.Caching$CachingProviderRegistry$1.run(Caching.java:442)
at java.security.AccessController.doPrivileged(Native Method)
at javax.cache.Caching$CachingProviderRegistry.getCachingProviders(Caching.java:442)
at javax.cache.Caching$CachingProviderRegistry.getCachingProvider(Caching.java:529)
at javax.cache.Caching$CachingProviderRegistry.getCachingProvider(Caching.java:476)
at javax.cache.Caching.getCachingProvider(Caching.java:226)
at org.hibernate.cache.jcache.internal.JCacheRegionFactory.getCachingProvider(JCacheRegionFactory.java:246)
at org.hibernate.cache.jcache.internal.JCacheRegionFactory.resolveCacheManager(JCacheRegionFactory.java:204)
at org.hibernate.cache.jcache.internal.JCacheRegionFactory.prepareForUse(JCacheRegionFactory.java:188)
at org.hibernate.cache.spi.AbstractRegionFactory.start(AbstractRegionFactory.java:91)
at org.hibernate.cache.internal.EnabledCaching.(EnabledCaching.java:77)
at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:33)
at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:24)
at org.hibernate.service.spi.SessionFactoryServiceInitiator.initiateService(SessionFactoryServiceInitiator.java:30)
at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:68)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.getService(SessionFactoryServiceRegistryImpl.java:109)
at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:239)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:939)
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:141)
at org.glassfish.persistence.jpa.PersistenceUnitLoader.loadPU(PersistenceUnitLoader.java:199)
at org.glassfish.persistence.jpa.PersistenceUnitLoader.(PersistenceUnitLoader.java:107)
at org.glassfish.persistence.jpa.JPADeployer$1.visitPUD(JPADeployer.java:223)
at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:230)
at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)
at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:925)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:434)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:360)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:360)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)]]
[2019-07-03T14:57:07.478+0200] [glassfish 4.1] [SEVERE] [] [javax.enterprise.system.core] [tid: _ThreadID=53 _ThreadName=admin-listener(5)] [timeMillis: 1562158627478] [levelValue: 1000] [[
Exception while preparing the app]]
[2019-07-03T14:57:07.478+0200] [glassfish 4.1] [SEVERE] [NCLS-CORE-00026] [javax.enterprise.system.core] [tid: _ThreadID=53 _ThreadName=admin-listener(5)] [timeMillis: 1562158627478] [levelValue: 1000] [[
Exception during lifecycle processing
java.util.ServiceConfigurationError: javax.cache.spi.CachingProvider: Provider com.hazelcast.cache.HazelcastCachingProvider not a subtype
at java.util.ServiceLoader.fail(ServiceLoader.java:239)
at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:376)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
at javax.cache.Caching$CachingProviderRegistry$1.run(Caching.java:448)
at javax.cache.Caching$CachingProviderRegistry$1.run(Caching.java:442)
at java.security.AccessController.doPrivileged(Native Method)
at javax.cache.Caching$CachingProviderRegistry.getCachingProviders(Caching.java:442)
at javax.cache.Caching$CachingProviderRegistry.getCachingProvider(Caching.java:529)
at javax.cache.Caching$CachingProviderRegistry.getCachingProvider(Caching.java:476)
at javax.cache.Caching.getCachingProvider(Caching.java:226)
at org.hibernate.cache.jcache.internal.JCacheRegionFactory.getCachingProvider(JCacheRegionFactory.java:246)
at org.hibernate.cache.jcache.internal.JCacheRegionFactory.resolveCacheManager(JCacheRegionFactory.java:204)
at org.hibernate.cache.jcache.internal.JCacheRegionFactory.prepareForUse(JCacheRegionFactory.java:188)
at org.hibernate.cache.spi.AbstractRegionFactory.start(AbstractRegionFactory.java:91)
at org.hibernate.cache.internal.EnabledCaching.(EnabledCaching.java:77)
at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:33)
at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:24)
at org.hibernate.service.spi.SessionFactoryServiceInitiator.initiateService(SessionFactoryServiceInitiator.java:30)
at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:68)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.getService(SessionFactoryServiceRegistryImpl.java:109)
at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:239)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:939)
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:141)
at org.glassfish.persistence.jpa.PersistenceUnitLoader.loadPU(PersistenceUnitLoader.java:199)
at org.glassfish.persistence.jpa.PersistenceUnitLoader.(PersistenceUnitLoader.java:107)
at org.glassfish.persistence.jpa.JPADeployer$1.visitPUD(JPADeployer.java:223)
at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:230)
at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)
at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:925)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:434)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:360)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:360)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
]]
[2019-07-03T14:57:07.569+0200] [glassfish 4.1] [SEVERE] [] [javax.enterprise.system.core] [tid: _ThreadID=53 _ThreadName=admin-listener(5)] [timeMillis: 1562158627569] [levelValue: 1000] [[
Exception while preparing the app : javax.cache.spi.CachingProvider: Provider com.hazelcast.cache.HazelcastCachingProvider not a subtype]]

@supralog-ecc thanks for the issue report. The exception looks fishy. HazelcastCachingProvider does implement javax.cache.spi.CachingProvider.

I am not familiar with the specifics of configuring Hibernate & JCache but my guess is that there is some classloading issue at play here. Can you check both your app-server-wide classpath and your web app's classpath for occurrences of cache-api JAR?

@vbekiaris thanks for your prompt response. We've continued for searching the cause following your advice about classloading issue, we tried to debug a part of code in hazelcast-hibernate53 by replacing
final ClassLoader cl = Thread.currentThread().getContextClassLoader(); try { final Class<IHazelcastInstanceFactory> factory = (Class<IHazelcastInstanceFactory>) Class.forName(factoryName, true, cl);
by
final ClassLoader cl = IHazelcastInstanceFactory.class.getClassLoader(); try { final Class<IHazelcastInstanceFactory> factory = (Class<IHazelcastInstanceFactory>) Class.forName(factoryName, true, cl);
Then the project works for the deployment! This is only tested in a debug mode, the problem persists in our project.

Is it a proper/safe way to use Thread.currentThread().getContextClassLoader() rather than IHazelcastInstanceFactory.class.getClassLoader()?

Maybe it's better to ask this question in the project hazelcast-hibernate53.

@supralog-ecc thanks for the helpful update, it seems this is a change from hazelcast-hibernate4 class loader choice.

I transferred the issue to hazelcast-hibernate5 repository so others with better knowledge of hibernate integration lifecycle can comment on the choice of class loader /cc @leszko @mesutcelik ?

Hi @supralog-ecc

I could not reproduce it with the versions you provided. Can you please send a reproducer?

Using HazelcastCachingProvider as hibernate.javax.cache.provider the second level cache works well with Hazelcast. Here is all I have done for configuring it and the properties are the same as yours.

Configuration configuration = new Configuration()
.setProperty("hibernate.cache.use_second_level_cache", "true") 
.setProperty("hibernate.cache.region.factory_class","org.hibernate.cache.jcache.JCacheRegionFactory")	
.setProperty("hibernate.javax.cache.provider", "com.hazelcast.cache.HazelcastCachingProvider")
...
...

I've had a look at this one and could not reproduce. Closing for now. Don't hesitate to reopen should the issue reoccur.