如何使用Atomikos Transaction Essentials和Hibernate> = 4.3

时间:2013-12-19 11:59:46

标签: hibernate jta atomikos

我从Hibernate 4.2切换到Hibernate 4.3,我的项目不再工作了。我得到了一个

  

HibernateException:无法找到当前的JTA事务

当我做的时候

Session s = sessionFactory.getCurrentSession();

我意识到org.hibernate.transaction.TransactionManagerLookup不再存在。它在Hibernate 4.3中被删除了。我该如何更改当前配置?

<hibernate-configuration>
<session-factory>
    <property name="connection.datasource">testDS</property>

    <property name="current_session_context_class">jta</property>
    <property name="transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</property>
    <property name="transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
    <property name="connection.release_mode">auto</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

    <property name="hibernate.hbm2ddl.auto">create-drop</property>
    <property name="hibernate.show_sql">true</property>
    <mapping class="test.entities.Person" />
    <mapping class="test.entities.CreditCard" />
    <mapping class="test.entities.ExampleRevEntity" />
</session-factory>

5 个答案:

答案 0 :(得分:7)

在Hibernate 4.3中,已删除已弃用的长TransactionManagerLookup。现在,JTA提供程序必须实现org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform。 Hibernate中已经提供了JTA Platform的抽象实现,即org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform。使用它,为Atomikos编写JTA平台非常简单:

package test;

import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform;
import com.atomikos.icatch.jta.UserTransactionManager;

public class AtomikosJtaPlatform extends AbstractJtaPlatform {

  private static final long serialVersionUID = 1L;
  private UserTransactionManager utm;

  public AtomikosJtaPlatform() {
    utm = new UserTransactionManager();
  }

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

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

此外,必须将平台实现类的名称添加到hibernate配置中:

<property name="hibernate.transaction.jta.platform">test.AtomikosJtaPlatform</property>

答案 1 :(得分:6)

使用Hibernate JTA Platform与Spring编写并编译此代码

package my.domain.spring.hibernate.jta;

import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;

import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform;
import org.springframework.transaction.jta.JtaTransactionManager;
import org.springframework.util.Assert;

@SuppressWarnings("serial")
public class SpringJtaPlatformAdapter extends AbstractJtaPlatform {

    private static TransactionManager sTransactionManager;
    private static UserTransaction sUserTransaction;


    @Override
    protected TransactionManager locateTransactionManager() {
        Assert.notNull(sTransactionManager, "TransactionManager has not been setted");
        return sTransactionManager;
    }


    @Override
    protected UserTransaction locateUserTransaction() {
        Assert.notNull(sUserTransaction, "UserTransaction has not been setted");
        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;
    }

}

添加到弹簧配置

  <bean id="txObjcoreManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager">
            <bean class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
                <property name="forceShutdown" value="true" />
            </bean>
        </property>
        <property name="userTransaction">
            <bean class="com.atomikos.icatch.jta.UserTransactionImp">
                <property name="transactionTimeout" value="300" />
            </bean>
        </property>
    </bean>

    <bean id="springJtaPlatformAdapter" class="my.domain.spring.hibernate.jta.SpringJtaPlatformAdapter">
        <property name="jtaTransactionManager" ref="txObjcoreManager" />
    </bean>

不要忘记添加依赖

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        depends-on="springJtaPlatformAdapter">

最后更改像这样的hibernate.transaction.jta.platform=my.domain.spring.hibernate.jta.SpringJtaPlatformAdapter

的hi​​bernate配置

答案 2 :(得分:2)

Spring用户的一些提示 - 如果使用工厂bean设置东西,只需使用此实现:

public class AtomikosPlatform extends AbstractJtaPlatform {

  private static final long serialVersionUID = -1L;

  @Override
  protected TransactionManager locateTransactionManager() {
    return new J2eeTransactionManager();
  }

  @Override
  protected UserTransaction locateUserTransaction() {
    return new J2eeUserTransaction();
  }

}

答案 3 :(得分:1)

您可以尝试将org.springframework.orm.hibernate4.LocalSessionFactoryBean的jtaTransactionManager属性设置为Spring的JtaTransactionManager吗?我有类似的问题,但由此解决。顺便说一下,HibernateTemplate又回到了Spring 4.0.1。虽然不推荐,但我喜欢使用它。它帮助处理了很多事情。 (我使用的是Spring 4.0.5 + Hibernate 4.3.5 + Atomikos 3.9.3)

<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close" depends-on="userTransactionService">
   <property name="forceShutdown" value="true" />
</bean>

<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp" depends-on="userTransactionService">
   <property name="transactionTimeout" value="180" />
</bean>

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

<bean id="rentSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource"><ref bean="rentXADataSource" /></property>
    <property name="mappingLocations" value="classpath:com/kj/model/web/*.hbm.xml"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</prop>          
            <prop key="hibernate.cache.use_query_cache">true</prop> 
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.format_sql">false</prop>
            <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
        </props>
    </property>
    <property name="jtaTransactionManager" ref="JtaTransactionManager"></property>
</bean>

答案 4 :(得分:0)

以下是一种适用于Spring配置的替代方法。这与Anton的方法不同,因为它不依赖于实例方法写入静态字段(这通常被认为是不好的做法)。

@SuppressWarnings("serial")
public class AtomikosJtaPlatform extends AbstractJtaPlatform {

    private static TransactionManager transactionManager;
    private static UserTransaction userTransaction;

    public static void factoryInit(TransactionManager transactionManager, UserTransaction userTransaction) {
        AtomikosJtaPlatform.transactionManager = transactionManager;
        AtomikosJtaPlatform.userTransaction = userTransaction;
    }

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

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

}

然后在Spring配置中:

    <bean id="AtomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init"
        destroy-method="close">
        <property name="startupTransactionService" value="false" />
        <property name="forceShutdown" value="false" />
    </bean>

    <bean id="AtomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
        <property name="transactionTimeout" value="300" />
    </bean>

    <!-- AtomikosJtaPlatform is created by Hibernate using reflection. This ensures it uses our Spring configured beans --> 
    <bean id="JtaPlatformInitializer" class="org.springframework.beans.factory.config.MethodInvokingBean">
        <property name="targetClass" value="com.mycompany.a.b.AtomikosJtaPlatform" />
        <property name="targetMethod" value="factoryInit" />
        <property name="arguments">
            <list>
                <ref bean="AtomikosTransactionManager" />
                <ref bean="AtomikosUserTransaction" />
            </list>
        </property>
    </bean>

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="JtaPlatformInitializer">
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.transaction.jta.platform">com.mycompnay.a.b.AtomikosJtaPlatform</prop>
 ...