Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
910 views
in Technique[技术] by (71.8m points)

jta - How to use Atomikos Transaction Essentials with Hibernate >= 4.3

I switched from Hibernate 4.2 to Hibernate 4.3 and my project is not working any more. I'm getting an

HibernateException: Unable to locate current JTA transaction

when I do

Session s = sessionFactory.getCurrentSession();

I've realized that org.hibernate.transaction.TransactionManagerLookup does not exist any more. It was deleted in Hibernate 4.3. How should I change my current configuration?

<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>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Hibernate 4.3 the long deprecated TransactionManagerLookup got removed. Now the JTA provider must implement org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform. An abstract implementation of JTA Platform is already available within Hibernate namely org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform. Using this it is quite simple to write a JTA Platform for Atomikos:

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

In addition the name of the platform implementation class must be added to the hibernate configuration:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...