简体   繁体   中英

JTA with Spring 4.2 and Hibernate 5.0 (JPA interface)

Recently, other developers where I work have upgraded our aging Hibernate dependencies from 3.? (maybe 3.5?) to 5.0. They noticed shortly afterward that we were getting connection leaks, detected by DBCP. I was tasked with trying to diagnose the cause.

I decided to try and upgrade Spring as well, since that was neglected, and I have replaced all the relevant jars to upgrade from 3.0.5 to 4.2.5, and now am seeing a different issue that I can't figure out.

We use JOTM for JTA, and are presently using version 2.0.11, according to the MANIFEST.MF file in the jar. With this version, combined with the updated Hibernate and Spring, I'm seeing the following:

org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException: Could not obtain JOTM transaction manager instance
        at org.hibernate.engine.transaction.jta.platform.internal.JOTMJtaPlatform.locateTransactionManager(JOTMJtaPlatform.java:31)
        at org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform.retrieveTransactionManager(AbstractJtaPlatform.java:92)
        at org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform.getTransactionManager(AbstractJtaPlatform.java:98)
        at org.hibernate.engine.transaction.jta.platform.internal.TransactionManagerBasedSynchronizationStrategy.canRegisterSynchronization(TransactionManagerBasedSynchronizationStrategy.java:39)
        at org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform.canRegisterSynchronization(AbstractJtaPlatform.java:131)
        at org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl.pulse(JtaTransactionCoordinatorImpl.java:141)
        at org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl.<init>(JtaTransactionCoordinatorImpl.java:92)
        at org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl.buildTransactionCoordinator(JtaTransactionCoordinatorBuilderImpl.java:28)
        at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:274)
        at org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1327)
        at org.hibernate.jpa.internal.EntityManagerImpl.internalGetSession(EntityManagerImpl.java:133)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:174)
        at org.hibernate.jpa.internal.EntityManagerImpl.<init>(EntityManagerImpl.java:83)
        at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:319)
        at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:286)
        at sun.reflect.GeneratedMethodAccessor399.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:407)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:560)
        at com.sun.proxy.$Proxy243.createEntityManager(Unknown Source)
        at org.springframework.orm.jpa.EntityManagerFactoryUtils.doGetTransactionalEntityManager(EntityManagerFactoryUtils.java:285)
        at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:255)
        at com.sun.proxy.$Proxy271.createQuery(Unknown Source)
        at com.company.app.persistence.AbstractEntityGroupDAO.getSomething(AbstractEntityGroupDAO.java:80)
        at com.company.app.servicehealth.StatusServiceDAO.retrieveStatusServices(StatusServiceDAO.java:259)
        at com.company.app.servicehealth.StatusServiceDAO.queryForStatusServices(StatusServiceDAO.java:255)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
        at com.sun.proxy.$Proxy273.queryForStatusServices(Unknown Source)
        at com.company.app.servicehealth.ServiceHealthStatusImpl$1.doInTransaction(ServiceHealthStatusImpl.java:142)
        ... 50 more
Caused by: java.lang.NullPointerException
        at org.hibernate.engine.transaction.jta.platform.internal.JOTMJtaPlatform.locateTransactionManager(JOTMJtaPlatform.java:26)
        ... 90 more

Upon inspection, this looks to me as if either serviceRegistry() or serviceRegistry().getService( ClassLoaderService.class ) is returning null. (Below is the contents of the method in question)

public static final String TM_CLASS_NAME = "org.objectweb.jotm.Current";
public static final String UT_NAME = "java:comp/UserTransaction";

@Override
protected TransactionManager locateTransactionManager() {
    try {
        final Class tmClass = serviceRegistry().getService( ClassLoaderService.class ).classForName( TM_CLASS_NAME );
        final Method getTransactionManagerMethod = tmClass.getMethod( "getTransactionManager" );
        return (TransactionManager) getTransactionManagerMethod.invoke( null, (Object[]) null );
    }
    catch (Exception e) {
        throw new JtaPlatformException( "Could not obtain JOTM transaction manager instance", e );
    }
}

How can I go about solving this? I'm absolutely new to Spring, and all I can say is that we're using JPA (EntityManager) instead of Hibernate's SessionFactory.

Sounds like a dependency issue. In your POM dependency hierarchy (assuming you use Maven), check to see if you have multiple versions of Hibernate in your classpath. You may want to consider trying Hibernate 4.3.11.Final instead of 5.x .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM