我正在运行spring 4.1.4,hibernate 4.3.8,atomikos 3.9.3,java 8,tomcat 8。

启动服务器时,我在localhost.log中看到上述异常,但是除了我已经配置的位置之外,我不确定在哪里配置TransactionManagerLookup。在升级 hibernate 之前并未发生这种情况,因此很可能是版本问题。有人可以帮忙吗?

仅供引用:catalina.out没什么用。只是:

SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal Error listenerStart
SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous errors

我的appContext定义了:
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</prop>
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>

和完整的堆栈跟踪是:
22-Jan-2015 10:07:25.734 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class com.my.app.web.InitializerListener
 org.hibernate.HibernateException: No TransactionManagerLookup specified
    at org.hibernate.context.internal.JTASessionContext.currentSession(JTASessionContext.java:85)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
    at com.my.app.dao.AbstractMyDAO.currentSession(AbstractMyDAO.java:116)
    at com.my.app.dao.AbstractMyDAO.criteria(AbstractMyDAO.java:86)
    at com.my.app.dao.AbstractMyDAO.count(AbstractMyDAO.java:79)
    at com.my.app.initialize.MyInitializerImpl.initializeApplicaiton(MyInitializerImpl.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    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:207)
    at com.sun.proxy.$Proxy220.initializeApplicaiton(Unknown Source)
    at com.my.app.web.MyInitializerListener.contextInitialized(MyInitializerListener.java:42)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4772)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:917)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1701)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

编辑:Atomikos事务管理器是这样配置的
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
    <property name="forceShutdown" value="true" />
    <!--  in secs -->
    <property name="transactionTimeout" value="300"/>
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
    <!--  in secs -->
    <property name="transactionTimeout" value="300" />
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager" ref="atomikosTransactionManager" />
    <property name="userTransaction" ref="atomikosUserTransaction" />
    <property name="transactionSynchronizationName" value="SYNCHRONIZATION_ON_ACTUAL_TRANSACTION" />
    <property name="allowCustomIsolationLevels" value="true"/>
</bean>

编辑2:

我想我需要在这里澄清一些事情。
我有一个名为AbstractMyDAO的DAO对象(从堆栈中可以看到)。在这个对象中定义了一个 session 工厂
@Autowired
private SessionFactory mySessionFactory;

调用currentSession时,将引发上述错误,因为 hibernate 无法找到与此 session 工厂关联的事务管理器查找。
public Session currentSession() {
    return mySessionFactory.getCurrentSession();
}

该 session 工厂在我的应用上下文文件中定义为:
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource">
        <ref bean="myDataSource" />
    </property>
    <property name="annotatedClasses">
        <list>
            <value>some values... </value>
            ...
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                ${gst.hibernate.dialect}
            </prop>
            <prop key="query.factory_class">
                org.hibernate.hql.ast.ASTQueryTranslatorFactory
            </prop>
            <prop key="hibernate.max_fetch_depth">3</prop>
            <prop key="hibernate.show_sql">${my.hibernate.showsql}</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.use_structured_entries">true</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
            <prop key="net.sf.ehcache.configurationResourceName">${ehcache.my.persist.config}</prop>
            <prop key="hibernate.connection.isolation">3</prop>
            <prop key="connection.release_mode">auto</prop>
            <prop key="hibernate.current_session_context_class">jta</prop>
            <prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</prop>
            <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
            <prop key="hibernate.search.default.directory_provider">filesystem</prop>
            <prop key="hibernate.search.default.indexBase">/var/log/my/lucene/indexes</prop>
        </props>
    </property>
</bean>

如您所见,属性hibernate.transaction.manager_lookup_class已定义,但在创建bean时找不到,我也不知道为什么。配置是否更改?

编辑3:
当我调试时:
最终的JtaPlatform jtaPlatform = factory()。getServiceRegistry()。getService(JtaPlatform.class);

它返回一个NoJTAPlatform。我想这是我的问题。

编辑4:
似乎没有实现适合Tomcat的JtaPlatform的任何东西,或者我是否介意?

最佳答案

确保还配置了Atomikos事务管理器,以便TransactionManagerLookup可以相应地找到UserTransaction:

<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
    init-method="init" destroy-method="close">
    <property name="transactionTimeout" value="300" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
    <property name="transactionTimeout" value="300" />
</bean>

<bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager" ref="atomikosTransactionManager" />
    <property name="userTransaction" ref="atomikosUserTransaction" />
</bean>

然后,您也应该使用Spring EntityManager工厂:
<bean id="entityManager"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:dataSource-ref="dataSource"
    p:persistenceXmlLocation="**/persistence.xml"
    p:persistenceUnitName="persistenceUnit"
    depends-on="dataSource, transactionManager">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</prop>
            <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
            ...
        </props>
    </property>
</bean>

然后,DAO应该注入(inject)EntityManager:
@PersistenceContext(unitName = "persistenceUnit")
private EntityManager entityManager;

而不是直接调用currentSession(AbstractMyDAO):
at com.my.app.dao.AbstractMyDAO.currentSession(AbstractMyDAO.java:116)

09-27 14:57