Another unnamed CacheManager already exists in the same VM (ehCache 2.5) - spring

This is what happens when I run my junit tests...
Another CacheManager with same name 'cacheManager' already exists in the same VM. Please
provide unique names for each CacheManager in the config or do one of 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 ]
What's the reason behind the exception. Could there be more than 1 cacheManager running simultaneously?
This is how I configured the cachManager using Sping 3.1.1. It sets explicitly the scope of the cacheManager to "singleton"
<ehcache:annotation-driven />
<bean
id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
scope="singleton"
/>
The ehcache.xml looks like
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false"
maxBytesLocalHeap="100M"
name="cacheManager"
>
....
</ehcache>
Finally my class
#Component
public class BookingCache implements CacheWrapper<String, BookingUIBean> {
#Autowired
private CacheManager ehCacheManager;
....
}
I'm very sure that I'm dealing with only one cacheManager in my code base. Something else is probably running the n-th instance.

Your EhCacheManagerFactoryBean may be a singleton, but it's building multiple CacheManagers and trying to give them the same name. That violates Ehcache 2.5 semantics.
Versions of Ehcache before version 2.5 allowed any number of CacheManagers with the same name (same configuration resource) to exist in a JVM.
Ehcache 2.5 and higher does not allow multiple CacheManagers with the same name to exist in the same JVM. CacheManager() constructors creating non-Singleton CacheManagers can violate this rule
Tell the factory bean to created a shared instance of the CacheManager in the JVM by setting the shared property to true.
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:shared="true"/>

I had the same issue with my integration tests using JPA (2.0) + Hibernate (3.6.4) + Spring (3.2.4).
The issue was resolved using following Hibernate configuration:
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory"/>
instead of using
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>

Your problem is the context loading optimization built in the Spring test framework. Spring (per default) does not destroy the context once the test class is done, in hope that another test class might reuse it (instead of creating it from scratch).
You can override this default using #DirtiesContext, or if you use maven you can set surefire forkMode to "always" and create a new VM per test class.

After upgrading to Hibernate 5 I had to use:
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
instead of:
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory"/>
Please note that the packages differ from each other.

You may also try to set name"xxx" on your ehcache.xml configuration (on the ehcache element).
That did the trick for me, as I think I had another cache configuration lurking in one of the modules of my app.
The shared solution also works, but I don't know the far-ranging implications of that.
http://forums.terracotta.org/forums/posts/list/6495.page
https://norrisshelton.wordpress.com/2012/03/08/spring-3-1-caching-abstraction-with-ehcache/

For posterity: A better way is to use the "accept-existing" property of the EhCacheManagerFactoryBean.

Setting the EhCacheManagerFactoryBean#shared to true worked for me.
Setting the EhCacheManagerFactoryBean#acceptExisting to true DIDN'T work for me.
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
#Configuration
public class EhCacheConfiguration {
#Bean
public EhCacheCacheManager ehCacheCacheManager() {
return new EhCacheCacheManager(ehCacheManagerFactoryBean().getObject());
}
#Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
cacheManagerFactoryBean.setShared(true);
return cacheManagerFactoryBean;
}
}
As explained in Using EhCache in Spring 4 without XML

if you just test your business service,not second level cache,you can remove second level configuration in your spring config file,your test will be run successfully. there is my second level configuration :
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="defaultPU" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">false</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
</props>
</property>
</bean>
if i change to full configuration of second level cache config ,the real webapp use in running time,like this:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="defaultPU" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">false</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
<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">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">ehcache/ehcache-hibernate-local.xml</prop>
</props>
</property>
</bean>
then i get the same Exception "Another unnamed CacheManager already exists in the same VM"

In my case, we have a custom cache manager defined as bean.
Also a custom application context so we don't use the spring junit runner... hence the #DirtiesContext doesn't work.
The trick is to retrieve the cache instance from the bean, on that cache get the cacheManager (the instance from EHCache). and on that cachemanager call the removeCache method.
Put this in a method annotated with #After and your cache is removed from the VM after each test. Like this:
#After
public void destroy() {
MyCustomCacheManager customCacheManager = (MyCustomCacheManager) context.getBean("yourCustomCacheManagerBean");
try {
net.sf.ehcache.Cache cache = customCacheManager.getCache();
net.sf.ehcache.CacheManager cacheManager = cache.getCacheManager();
cacheManager.removeCache("nameOfYourCache");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
context.destroy();
context = null;
}

I solved it by adding following to resources.groovy :
beans = {
...
aclCacheManager(EhCacheManagerFactoryBean) {
shared = true
}
...
}

It happened to me when switching to Spring Boot 2.0.2. Resolved it by doing the following:
REMOVE in application.yml
spring.jpa.properties.hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
REMOVE in pom.xml
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
KEEP in pom.xml only
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
</dependency>

For future readers, the cause of this problem in my case was that in my pom.xml file I had imported the hibernate-ehcache library, which unknown to me also already contained the ehcache library, and then explicitly imported the net.sf.ehache libray.
This seemed to work fine when I was running as a standalone app (a command line utility for example) but it caused the error in the original post when running on a tomcat server.
Changing my pom file from:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.7.4</version>
</dependency>
To:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.0.2.Final</version>
</dependency>
<!-- ehcache dependency removed -->
Fixed the problem. If anyone has any idea why the problem only appeared when running in a tomcat container I'd be interested to know..

In glassfish 3.0.1, I traced the issue to IniShiroFilter getting initialize twice, which happens when concurrent requests are fired just after the server start. Following is a stack trace from two different threads corresponding to two HTTP requets:
[#|2012-11-28T08:25:10.630-0800|SEVERE|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=28;_ThreadName=Thread-1;|java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Thread.java:1249)
at org.apache.shiro.web.servlet.IniShiroFilter.<init>(IniShiroFilter.java:124)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:303)
at com.sun.enterprise.web.WebContainer.createFilterInstance(WebContainer.java:725)
at com.sun.enterprise.web.WebModule.createFilterInstance(WebModule.java:1948)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:248)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
at com.sentilla.filter.DumpFilter.doFilter(DumpFilter.java:152)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:322)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:239)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:662)
Another thread
[#|2012-11-28T08:25:15.299-0800|SEVERE|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=29;_ThreadName=Thread-1;|java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Thread.java:1249)
at org.apache.shiro.web.servlet.IniShiroFilter.<init>(IniShiroFilter.java:124)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:303)
at com.sun.enterprise.web.WebContainer.createFilterInstance(WebContainer.java:725)
at com.sun.enterprise.web.WebModule.createFilterInstance(WebModule.java:1948)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:248)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
at com.sentilla.filter.DumpFilter.doFilter(DumpFilter.java:152)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:322)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:239)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:662)
Looking at the stack trace ApplicationFilterConfig.java:248 could be the culprit. Or, glassfish is initializing filters in the wrong context, for comparison, Tomcat initializes filters during BootStrap.

In my case Problem is component-scan and java config.
root-context.xml
<context:component-scan base-package="org.beansugar">
servlet-context.xml
<context:component-scan base-package="org.beansugar">
spring component-scan work two times on xml files.
it generate beans inside SpringConfig.java each run time.
then duplicate cache manager was created.
so, I changed that like below.
root-context.xml
<context:component-scan base-package="org.beansugar">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
servlet-context.xml
<context:component-scan base-package="org.beansugar" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

This error also happens with wrong mapping files. The message is horrible, doesn't say the cause.

In my case configuration was as follows:
<spring.boot.version>1.5.8.RELEASE</spring.boot.version>
<spring.boot.yarn.version>2.4.0.RELEASE</spring.boot.yarn.version>
<spring.version>4.3.7.RELEASE</spring.version>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.5.1-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>3.5.1-Final</version>
</dependency>
Changing the EHCache provider class did the job for me. I was using cache provider class as org.hibernate.cache.EhCacheProvider instead i changed this to:
net.sf.ehcache.hibernate.SingletonEhCacheProvider

As of Spring Boot 2.1.2 the following configuration worked to resolve the issue. (Note, these are snippets of the overall config.)
Dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.2.8.Final</version>
</dependency>
application.yml config:
spring:
jpa:
open-in-view: false
hibernate:
ddl-auto: none
show-sql: true
properties:
dialect: org.hibernate.dialect.MySQLDialect
net:
sf:
ehcache:
configurationResourceName: ehcache.xml
hibernate:
cache:
use_second_level_cache: true
region:
factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
ehcache.xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- Required elements -->
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"/>
<!-- Cache settings per class -->
<cache name="com.mystuff.component.services.example.Book"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"/>
</ehcache>
The application I am working on slows down drastically without a working cache. So, to validate I simply ran the application and hit one of the read intense endpoints.

In my case the Manager was created by this bean (ehCache 2.10):
<bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="shared" value="false"/>
</bean>
the only solution that worked for me is to destroy it manually in that way :
#Inject
private EhCacheManagerFactoryBean ehCacheManagerFactoryBean;
And then
ehCacheManagerFactoryBean.destroy();

Related

HazelCast Hibernate52 - Throws Unable to resolve name com.hazelcast.hibernate.HazelcastCacheRegionFactory

I'm trying to create a second level cache using hazelcast-hibernate52 (which supports hibernate 5.2.12.Final, I guess) when I run it throws the following exception
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [com.hazelcast.hibernate.HazelcastCacheRegionFactory] as strategy [org.hibernate.cache.spi.RegionFactory]
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:126)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:194)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:161)
at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:67)
at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:28)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
... 47 more
Below are my Spring hazel configurations
<hz:hazelcast id="instance">
<hz:config>
<hz:instance-name>hazelcast</hz:instance-name>
<!-- <hz:spring-aware /> -->
<hz:group name="dev" password="dev-password"/>
<hz:management-center enabled="true" url="http://localhost:9008/hazelcast-mancenter"/>
<hz:properties>
<hz:property name="hazelcast.merge.first.run.delay.seconds">5</hz:property>
<hz:property name="hazelcast.merge.next.run.delay.seconds">5</hz:property>
</hz:properties>
<hz:network port="5701" port-auto-increment="false" port-count="1">
<hz:join>
<!-- Enable for auto detection -->
<hz:multicast enabled="true" />
<hz:tcp-ip enabled="false">
<hz:members>120.0.0.1</hz:members>
</hz:tcp-ip>
</hz:join>
<hz:interfaces>
<hz:interface>120.0.0.*</hz:interface>
</hz:interfaces>
</hz:network>
These are my spring hibernate configurations
<bean id="tpSessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="tpDataSource"></property>
<property name="hibernateProperties">
<props>
<!-- <prop key="hibernate.dialect">${db.dialect}</prop> -->
<prop key="hibernate.show_sql">${db.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${db.hbm2ddl.auto}</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">com.hazelcast.hibernate.HazelcastCacheRegionFactory</prop>
<prop key="hibernate.cache.hazelcast.instance_name">hazelcast</prop>
</props>
and the dependencies which are used are:
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>3.10.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
<version>3.10.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-hibernate52</artifactId>
<version>1.2</version>
</dependency>
Is there anything which i'm missing in here?
#Clovrr, a couple of things I can see:
hibernate 5.2.12 support added in 1.2.2 version. Can you upgrade to 1.2.2 or latest 1.2.3 version?
Don't add hazelcast-all since it contains hazelcast-hibernate for Hibernate 3.X. Just use hazelcast-spring dependency, it'll get all necessary hazelcast dependencies.
Please check those & try again.
Problem with hazelcast-hibernate52 dependency.
Looks like the dependency isn't updating or could be some other reasons.
what I did is,
look the git code from here, built a jar out of it and included that into my classpath. Voila! everything seems to work well and good.

java.lang.ClassCastException: com.ibm.ws.tx.jta.TranManagerSet incompatible with javax.transaction.TransactionManager

I am getting class cast exception while trying to Index the entity through Hibernate5 Search API on Websphere 8.5.5.10. The transactions are being managed by Spring Transaction. The document entities are getting indexed properly on local tomcat but not on WebSphere.
I have created a RESTFUl web service call to index the entities.
Spring 4.2
Hibernate Core 5.0.11.Final
Hibernate Search 5.5.5.Final
Here is my config file
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="packagesToScan">
<list>
<value>model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.default_schema">dev</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.search.default.directory_provider">filesystem</prop>
<prop key="hibernate.search.default.indexBase">/var/lucene/indexes</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
And the stack trace in websphere app logs:-
HSEARCH000058: HSEARCH000116: Unexpected error during MassIndexer operation
org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException: Could not obtain WebSphere TransactionManager
at org.hibernate.engine.transaction.jta.platform.internal.WebSphereJtaPlatform.locateTransactionManager(WebSphereJtaPlatform.java:66)
at org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform.retrieveTransactionManager(AbstractJtaPlatform.java:87)
at org.hibernate.search.batchindexing.impl.BatchTransactionalContext.lookupTransactionManager(BatchTransactionalContext.java:53)
at org.hibernate.search.batchindexing.impl.BatchTransactionalContext.(BatchTransactionalContext.java:42)
at org.hibernate.search.batchindexing.impl.BatchIndexingWorkspace.runWithErrorHandler(BatchIndexingWorkspace.java:118)
at org.hibernate.search.batchindexing.impl.ErrorHandledRunnable.run(ErrorHandledRunnable.java:32)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:483)
at java.util.concurrent.FutureTask.run(FutureTask.java:274)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1157)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:627)
at java.lang.Thread.run(Thread.java:798)
Caused by: java.lang.ClassCastException: com.ibm.ws.tx.jta.TranManagerSet incompatible with javax.transaction.TransactionManager
at org.hibernate.engine.transaction.jta.platform.internal.WebSphereJtaPlatform.locateTransactionManager(WebSphereJtaPlatform.java:63)
I am not sure what am I doing wrong here. Apart from Index Webservice call, other REST webservices are running fine and my UI is populating as expected.
Any help will be much appreciated.
We ran into this issue.
Issue:
If we use a hibernate version that uses JPA 2.1 (> hibernate version 4.3), it will conflict with older versions of websphere (versions 7, 8.5 without the JPA 2.1 fix packs). Specifically the exception you would see is this:
nested exception is java.lang.NoSuchMethodError: javax/persistence/Table.indexes()[Ljavax/persistence/Index;
Solution 1:
One way to resolve this issue is setting the classloader to parent_last so that JPA 2.1 that ships with hibernate will be used. However, you will run into other problems like the ones mentioned above.
Conflict with TransactionManager that ships with hibernate, Exception:
java.lang.ClassCastException: com.ibm.ws.tx.jta.TranManagerSet incompatible
with javax.transaction.TransactionManager
This can be solved by excluding the following jar (example maven
configuration for the hibernate dependency) that causes the
conflict:
<exclusion>
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
<groupId>org.jboss.spec.javax.transaction</groupId>
</exclusion>
Conflict with xml-apis.jar that ships with hibernate resulting in the exception:
javax.xml.parsers.FactoryConfigurationError: Provider for javax.xml.parsers.DocumentBuilderFactory cannot be found
This can be resolved by excluding the xml-apis.jar from hibernate
dependency like so:
<exclusion>
<artifactId>xml-apis</artifactId>
<groupId>xml-apis</groupId>
</exclusion>
Solution 2:
Use a custom PersistenceProviderResolver so that the Websphere classes do not interfere with Hibernate:
https://stackoverflow.com/a/37354979/5150013
According to this article, this is a conflict between 2 different versions of java.transaction packages (one is in websphere, one in GemFire.jar, a lib deployed with websphere).
It also suggests a fix :
Drop the package javax.transaction from GemFire.jar: zip -d gemfire.jar javax/transaction/*
Hope this helps.
From SpringBoot version 2.2.0.RELEASE, there is transaction manager api used from jakarta. In case you want to make it work just exclude
<dependency>
<groupId>jakarta.transaction</groupId>
<artifactId>jakarta.transaction-api</artifactId>
</dependency>
from
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
The basic thought is to have only one transaction manager. Here we are removing the one being provided from the sprintboot-jpa

Spring Entitymanager java.lang.ClassFormatError

This is 2nd day that I am struggling with this issue.
We have a web app that uses spring and hibernate. Now We are trying to expose some functionality to command line. So We should be able to run a class from command line(without web app) so it should have access to spring and hibernate.
<context:annotation-config/>
<context:component-scan base-package="com.x.y.z"/>
<jpa:repositories base-package="com.x.y" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="jpaProperties">
<props>
<prop key="javax.persistence.validation.mode">NONE</prop>
<prop key="hibernate.validator.apply_to_ddl">false</prop>
<prop key="hibernate.validator.autoregister_listeners">false</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="database" value="SQL_SERVER" />
<property name="showSql" value="false" />
</bean>
</property>
</bean>
Here I am using LocalEntityManagerFactoryBean but it seems there is no way to set datasource to it. It gets from persistance.xml
If I use LocalContainerEntityManagerFactoryBean I can set datasource(it has datasource property)
persistance.xml:
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="oms-jpa">
<class>com.domain.User</class>
</persistence-unit>
In persistance.xml I am not defining any database informaton for datasource, so LocalEntityManagerFactoryBean does not work for this.
what I am getting:
Error creating bean with name 'entityManagerFactory' defined in file [C:\Users\ekamoliddinov\IdeaProjects\web\conf\spring-config.xml]: Invocation of init method failed; nested exception is java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/validation/Validation
googled then some says:
Jar versions messed up
jee-api.jar is missing
But I have jee-api.jar.
My questions:
What is causing the above exception?
Can I use LocalContainerEntityManagerFactoryBean without having web
container? Spec does not say anything about this.
If i can not use LocalContainerEntityManagerFactoryBean then how can I set up
LocalEntityManagerFactoryBean so it should use my datasource not from
persistance.xml(I can not define anything in persistance.xml).
I am using spring version 3.1.3 with hibernate 4.1.8.Final and i am in command line(I do no have jee environment)
Sorry about more than one questions, but all questions are arising from the same root I think.
Any help will be appreciated.
Thanks.
After trying 3 days I fixed the issue. The jars where messed up :)
It was because I have jee-api.jar in my classpath and I was running the code from command line, I had not web env. So there was not any impl of jee-api.jar in my env. That is why it was giving the above exception. I removed jee-api.jar and disappeared.
Another thing That I can tell: we can use LocalContainerEntityManagerFactoryBean from command line. I am using it.
The last thing: We can not set datasource to LocalEntityManagerFactoryBean
This my help to someone.

#Transactional in Spring+Hibernate

I an using Spring 3.1 + Hibernate 4.x in my web application.
In my DAO, i am saving User type object as following
sessionFactory.getCurrentSession().save(user);
But getting following exception:
org.hibernate.HibernateException: save is not valid without active transaction
I googled and found similar question on SO, with following solution:
Session session=getSessionFactory().getCurrentSession();
Transaction trans=session.beginTransaction();
session.save(entity);
trans.commit();
That solves the problem. But in that solution, there is lot of mess of beginning and committing the transactions manually.
Can't i use sessionFactory.getCurrentSession().save(user); directly without begin/commit of transactions manually?
I try to use #Transactional on my service/dao methods too, but the problem persists.
EDIT : Here is my Spring Config File:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${db.driverClassName}" p:url="${db.url}"
p:username="${db.username}" p:password="${db.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.myapp.entities" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>
I am using following Hibernate 4 dependencies:
<!-- Hibernate Dependency -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.1.Final</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
<scope>runtime</scope>
</dependency>
Please help.
Basically what needs to be done is to remove from the applicationContext.xml file the following line for Hibernate properties:
<prop key="hibernate.current_session_context_class">thread</prop>
Once that is remove, Hibernate makes use of Spring for transaction management
Good luck to you all.
i think you are using Hibernate 4.x then why you are using hibernate 3 transaction manager in application context file?
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
i think it should be
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
just try to use hibernate 4 transaction manager along with #Transactional attribute it should work.
Using your working version of the Spring XML, and the #Transactional annotated DAO class, are you defining the DAO in your Spring XML ? (Perhaps as a prototype) Because if you're not, then so far as I can see, your DAO is not going to be AOP'ed for transactional aspects. I think that's the easiest way. This example is from the Spring 3 Doc, section 10.5.6 Using #Transactional.
<!-- this is the service object that we want to make transactional -->
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"/>
...where you might substitute DefaultFooService for your DAO.

Autowiring not working in Spring 3.1.2, JUnit 4.10.0

Using Spring 3.1.2, JUnit 4.10.0, and pretty new to both versions. I'm having the problem that I can't get the annotation-based autowiring to work.
Below are two samples, the one not using annotations, which is working fine. And the second one using annotation, which doesn't work, and I don't find the reason. I followed the samples of spring-mvc-test pretty much.
Working:
package com.company.web.api;
// imports
public class ApiTests {
#Test
public void testApiGetUserById() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/com/company/web/api/ApiTests-context.xml");
UserManagementService userManagementService = (UserManagementService) ctx.getBean("userManagementService");
ApiUserManagementController apiUserManagementController = new ApiUserManagementController(userManagementService);
MockMvc mockMvc = standaloneSetup(apiUserManagementController).build();
// The actual test
mockMvc.perform(get("/api/user/0").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
}
}
Failing, because userManagementService is null, not getting autowired:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration // should default to ApiTests-context.xml in same package
public class ApiTests {
#Autowired
UserManagementService userManagementService;
private MockMvc mockMvc;
#Before
public void setup(){
// SetUp never gets called?!
}
#Test
public void testGetUserById() throws Exception {
// !!! at this point, userManagementService is still null - why? !!!
ApiUserManagementController apiUserManagementController
= new ApiUserManagementController(userManagementService);
mockMvc = standaloneSetup(apiUserManagementController).build();
// The actual test
mockMvc.perform(get("/api/user/0").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
}
}
Note that both test classes above should be using the same context configuration, and the userManagementService is defined in there.
ApiTests-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="user"/>
<property name="password" value="passwd"/>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource" p:mappingResources="company.hbm.xml">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
<context:annotation-config/>
<tx:annotation-driven/>
<context:mbean-export/>
<!-- tried both this and context:component-scan -->
<!--<bean id="userManagementService" class="com.company.web.hibernate.UserManagementServiceImpl"/>-->
<context:component-scan base-package="com.company"/>
<!-- Hibernate's JMX statistics service -->
<bean name="application:type=HibernateStatistics" class="org.hibernate.jmx.StatisticsService" autowire="byName"/>
</beans>
and the UserManagementService (interface) as well as UserManagementServiceImpl has the #Service annotation.
Two minor questions/observations: setup() never gets called, even though it has the #Before annotation. Furthermore, I noticed that my test methods don't get executed/recognized if they don't start with the name 'test', which is not the case though with all spring-mvc-test samples I saw.
pom.xml:
<dependency>
<groupId>org.junit</groupId>
<artifactId>com.springsource.org.junit</artifactId>
<version>4.10.0</version>
<scope>test</scope>
</dependency>
Update:
The problem only occurs when I run the tests from maven; it's ok when I run the test from within my IDE (IntelliJ IDEA).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.3</version>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
Autowiring wont happen unless you do a component scan.
Why have you commented it out in your code ?
<!--<context:component-scan base-package="com.company"/>-->
Also re:junit. If you're in eclipse can you just go to the dependency tree view of the pom and filter on junit. Check you're actually using that version and not pulling in an older junit.
Edit: Ok I just checked your config and was able to get it working this side. My only guess can be is that you're somehow running it with a bad test runner which is causing it to use the wrong junit.
Edit 2 (SOLVED): So it turns out that the problem is because you are using a custom version of junit. Surefire looks for the provided junit library and cant find it. As a result it defaults to junit 3, which is what causes your app to skip loading the config.
You can explictly specify the custom provider like
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.3</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.12.3</version>
</dependency>
</dependencies>
</plugin>
But I have found that it does not work well with custom repos. If possible I would suggest to use the standard version of junit.
Try specific context configuration, e.g.
#ContextConfiguration(locations = {"/file1.xml", "/file2.xml" })
(just showing how this can be used with multiple files when needed - one may be enough)
Edit: have you enabled AutowiredAnnotationBeanPostProcessor as mentioned here? http://www.mkyong.com/spring/spring-auto-wiring-beans-with-autowired-annotation/
I had this same problem. My #Autowire would work within my IDE (SpringSource STS) but would fail to load the application context when I was using Maven to build from the command line.
The issue was with my dependencies in the pom.xml. I was using the Spring version of JUnit which caused the error. I think this is the root cause of the original post. I didn't have to code any Maven plugin for it work.
I changed
<dependency>
<groupId>org.junit</groupId>
<artifactId>com.springsource.org.junit</artifactId>
<version>4.7.0</version>
</dependency>
to
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>

Resources