Hibernate jta

In this section, you will learn about jta with Hibernate.

Hibernate jta

In this section, you will learn about jta with Hibernate.


JTA stands for Java transaction API. It permits you to define transactions in a way which is free from transaction manager implementation. The Application Server applies the transaction manager with the JTS (Java Transaction Service). Still you can't call JTS functions directly. Rather, it calls the JTA function which afterward invokes the lower level JTS routines.

Why use JTA transaction ?

 The Java EE transaction manager controls a JTA transaction. The JTA transaction is useful where you need to update various database of different vendor. You can't work with different database using a specific DBMS's transaction manager.

JTA Limitation

Nested transaction is not supported by Java EE transaction manager. You can't initiate another transaction without ending the previously transaction.

JTA transaction methods

To perform a JTA transaction, you can implement begin, commit, and rollback methods of the javax.transaction.UserTransaction interface.


Given below sample code about how to employ JTA transactions with JBoss 6 server, Spring 3 and Hibernate 3 :


Notice carefully the <tx> and <bean> tag  of the below Spring configuration file :

<bean id="entityManagerFactory"
	<property name="persistenceUnitName" value="Writing" />

<bean id="writingDao" class="net.roseindia.dao.WritingDaoImpl" />

<bean id="writingBean" class="net.roseindia.beans.WritingBean" scope="singleton">
	<property name="writingDao" ref="writingDao" />

<tx:annotation-driven transaction-manager="myTransactionManager" />
<tx:jta-transaction-manager />

<bean id="myTransactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager" />

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor " />


Notice carefully the transaction-type of the <persistence-unit> tag and also first <property> tag of the  <properties> tag.

<persistence-unit name="Writing" transaction-type="JTA">

		<property name="hibernate.transaction.manager_lookup_class"
		value="org.hibernate.transaction.JBossTransactionManagerLookup" />
		<property name="hibernate.show_sql" value="true" />
		<property name="hibernate.format_sql" value="true" />
		<property name="hibernate.hbm2ddl.auto" value="create" />


The DAO implementation classes should be coded like this :

private EntityManager em;

@Transactional(readOnly = true)
public List<Tutorial> retrieveAllTutorials() {
	return em.createQuery("from Tutorial").getResultList();



Share on Google+Share on Google+

Hibernate jta

Posted on: April 19, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Hibernate jta  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image