Spring 5+ Hibernate 5 + Atomikos 4 + Tomcat的集成无法找到当前的JTA事务

时间:2018-05-22 14:07:58

标签: spring hibernate tomcat atomikos

我尝试了很多不同的解决方案,但获得了例外:

  

org.hibernate.HibernateException:无法找到当前的JTA   交易在   org.hibernate.context.internal.JTASessionContext.currentSession(JTASessionContext.java:75)   〜[hibernate-core-5.2.12.Final.jar:5.2.12.Final] at   org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:465)   〜[冬眠核-5.2.12.Final.jar:5.2.12.Final]

我正在使用Atomikos 4.0.4,Hibernate 5.2.12,Spring 5.0.2和Tomcat。

如何配置Spring 5 + Hibernate 5

这是我的配置

应用context.xml中

  

                    

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
            <prop key="net.sf.ehcache.configurationResourceName">/ehcache/ehcache.xml</prop>
            <prop key="hibernate.connection.datasource">java:/comp/env/jdbc/myoracle</prop>
            <prop key="hibernate.transaction.jta.platform">cgaweb.common.atomikos.SpringJtaPlatformAdapter</prop>
            <prop key="hibernate.max_fetch_depth">2</prop>
            <prop key="hibernate.connection.autocommit">false</prop>
            <prop key="hibernate.connection.release_mode">after_transaction</prop>
            <prop key="hibernate.current_session_context_class">jta</prop>
            <prop key="javax.persistence.transactionType">jta</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.id.new_generator_mappings">true</prop>
            <prop key="hibernate.mapping.precedence">class, hbm</prop>
            <prop key="connection.driver_class">oracle.jdbc.OracleDriver</prop>
        </props>
    </property>
</bean>

<!-- jta transaction -->
<bean id="atomikosTransactionService" class="com.atomikos.icatch.config.UserTransactionServiceImp" init-method="init" destroy-method="shutdownWait" >
<constructor-arg>
    <props>
        <prop key="com.atomikos.icatch.log_base_name">UserTransactionServiceImplog</prop>
    </props>
</constructor-arg>
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close" depends-on="atomikosTransactionService">
    <property name="forceShutdown" value="true"/>
    <property name="transactionTimeout" value="300" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp" depends-on="atomikosTransactionService">
    <property name="TransactionTimeout" value="300"/>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager" ref="atomikosTransactionManager"></property>
    <property name="userTransaction" ref="atomikosUserTransaction"></property>
    <property name="allowCustomIsolationLevels" value="true"></property>
</bean>
<!-- Manager lookup-->
<bean id="springJtaPlatformAdapter" class="cgaweb.common.atomikos.SpringJtaPlatformAdapter">
    <property name="jtaTransactionManager" ref="transactionManager" />
</bean>

<!-- post construct -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

springJtaPlatformAdapter的代码

public class SpringJtaPlatformAdapter extends AbstractJtaPlatform {

private static TransactionManager sTransactionManager;
private static UserTransaction sUserTransaction;


@Override
protected TransactionManager locateTransactionManager() {
    return sTransactionManager;
}

@Override
protected UserTransaction locateUserTransaction() {
    return sUserTransaction;
}

public void setJtaTransactionManager(JtaTransactionManager jtaTransactionManager) {
    sTransactionManager = jtaTransactionManager.getTransactionManager();
    sUserTransaction = jtaTransactionManager.getUserTransaction();
}


public void setTransactionManager(TransactionManager transactionManager) {
    sTransactionManager = transactionManager;
}


public void setUserTransaction(UserTransaction userTransaction) {
    sUserTransaction = userTransaction;
}

}

我正在使用tomcat,并添加以下lib:

  • 交易JTA-4.0.4.jar

  • 交易jdbc.4.0.4.jar

  • 交易hibernate4-4.0.jar

  • 交易API-4.0.4.jar

  • 交易-4.0.4.jar

  • JTA-1.1.jar

你能帮我解决这个问题吗? 谢谢

1 个答案:

答案 0 :(得分:0)

Atomikos的商业版包含内置的Tomcat集成和Hibernate 5支持:

https://www.atomikos.com/Main/ExtremeTransactionsFreeTrial

或者,您可以等待TransactionsEsssentials的5.0版本发布,其中还将包含Hibernate 5演示。我将没有内置的Tomcat集成。

最诚挚的问候