org.hibernate.HibernateException:在hibernate升级时没有指定TransactionManagerLookup

时间:2015-01-22 11:56:12

标签: java spring hibernate transactions atomikos

我正在运行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对象(从堆栈中可以看到)。在此对象中定义了一个会话工厂

@Autowired
private SessionFactory mySessionFactory;

当调用currentSession时,抛出上述错误,因为hibernate无法找到与此会话工厂关联的事务管理器查找。

public Session currentSession() {
    return mySessionFactory.getCurrentSession();
}

此会话工厂在我的应用程序上下文文件中定义:

<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: 当我调试时:     final JtaPlatform jtaPlatform = factory()。getServiceRegistry()。getService(JtaPlatform.class);

返回NoJTAPlatform。我想这是我的问题。

编辑4: 似乎没有任何东西可以实现适合tomcat的JtaPlatform或者我是精神上的吗?

3 个答案:

答案 0 :(得分:1)

确保您也配置了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应该注入EntityManager:

@PersistenceContext(unitName = "persistenceUnit")
private EntityManager entityManager;

而不是直接调用currentSession(AbstractMyDAO):

at com.my.app.dao.AbstractMyDAO.currentSession(AbstractMyDAO.java:116)

答案 1 :(得分:1)

我的问题的答案是here

&#34;在Hibernate 4.3中,已删除已弃用的长TransactionManagerLookup。现在,JTA提供者必须实现org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform。&#34;

答案 2 :(得分:1)

我通过

解决了

进入 generateEquipmentListByCell(printerCellMap, dryerCellMap) { const equipmentKey = {...printerCellMap, ...dryerCellMap}; if (!_.isEmpty(equipmentKey)) { return ( Object.keys(equipmentKey).map((key, index) => <GridColumn gridUnits={6}> <EquipmentCell printerList={printerCellMap[key]} dryerList={dryerCellMap[key]} index={index}/> </GridColumn> ) ); } 文件

更改

hibernate.cfg.xml

<property name="hibernate.current_session_context_class">thread</property>而不是thread

更多详细信息here