ClassNotFoundException: org.ehcache.jsr107.EhcacheCachingProvider with Second Level Cache with osgi/karaf/hibernate/Jcache/ehcache

Description

Error using Second Level Cache on OSGi/Karaf/hibernate/Jcache/ehcache

Using:

  • Hibernate 5.4.2

  • Karaf 4.2.5

  • Ehcache 3

  • Jcache 1.1.1

A bundle application on OSGi environment using karaf works fine if the second level cache is off, but if the property
hibernate.cache.use_second_level_cache is true there is an

Caused by: java.lang.ClassNotFoundException: org.ehcache.jsr107.EhcacheCachingProvider

Ehcache bundle is exporting EhcacheCachingProvider but is not accessible by hibernate classloader

1 2 3 4 5 6 service; [javax.cache.spi.CachingProvider] with properties: component.id = 20 component.name = org.ehcache.jsr107.EhcacheCachingProvider service.bundleid = 97 service.id = 170 service.scope = bundle

The problem is in class:

1 org.hibernate.cache.jcache.internal.JCacheRegionFactory trying to get a CachingProvider

In OSGI environments, the classloader used is not the org.hibernate.orm.jcache, instead is used sun.misc.Launcher$AppClassLoader

So, a java.lang.ClassNotFoundException: org.ehcache.jsr107.EhcacheCachingProvider is throwed by hibernate-jcache.

a test case:

https://github.com/klebeer/karaf-hibernate2Lcache-bug

Environment

JVM
Java Virtual Machine Java HotSpot(TM) 64-Bit Server VM version 25.211-b12
Version 1.8.0_211
Vendor Oracle Corporation

Karaf version 4.2.5
Ehcache 3.7.1

cache-api: 1.1.1

Status

Assignee

Unassigned

Reporter

Kleber Ayala

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

Yes, likely

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

5.4.2

Components

Affects versions

5.4.2

Priority

Major