Hibernate jta

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

Hibernate jta

Hibernate jta

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

JTA

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.

SAMPLE CODE

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

applicationContext.xml

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

<bean id="entityManagerFactory"
	class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
	<property name="persistenceUnitName" value="Writing" />
</bean>

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

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

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

persistence.xml

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">
	<provider>org.hibernate.ejb.HibernatePersistence</provider>
	<jta-data-source>java:/WritingDS</jta-data-source>
	<class>net.roseindia.entities.Writing</class>

	<properties>
		<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" />
		</properties>
</persistence-unit>

DAO

The DAO implementation classes should be coded like this :

@PersistenceContext
private EntityManager em;

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