JTA与Spring 4.2和Hibernate 5.0(JPA接口)

时间:2016-05-04 14:27:40

标签: java spring hibernate jta

最近,我工作的其他开发人员已经将我们老化的Hibernate依赖关系从3升级了。 (也许3.5?)到5.0。不久之后,他们注意到我们正在接收DBCP检测到的连接泄漏。我的任务是试图诊断原因。

我决定尝试升级Spring,因为这被忽略了,我已经取代所有相关的jar从3.0.5升级到4.2.5,现在我看到了一个不同的问题,我可以'弄清楚。

根据jar中的MANIFEST.MF文件,我们使用JOTM进行JTA,目前正在使用2.0.11版本。有了这个版本,结合更新的Hibernate和Spring,我看到了以下内容:

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

经过检查,这看起来好像serviceRegistry()serviceRegistry().getService( ClassLoaderService.class )返回null。 (以下是相关方法的内容)

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 );
    }
}

我该如何解决这个问题?我对Spring完全不熟悉,我只能说我们使用的是JPA(EntityManager)而不是Hibernate的SessionFactory。

1 个答案:

答案 0 :(得分:0)

听起来像是依赖问题。在POM依赖关系层次结构中(假设您使用Maven),检查您的类路径中是否有多个版本的Hibernate。您可能需要考虑尝试使用Hibernate 4.3.11.Final而不是5.x