Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JTA support in Tomcat 6 for Hibernate?

Tags:

They recommend using JTA transaction support in Java EE environment.

But how to configure JTA in Tomcat6 so that Hibernate Session could use it ?

Starting with version 3.0.1, Hibernate added the SessionFactory.getCurrentSession() method. Initially, this assumed usage of JTA transactions, where the JTA transaction defined both the scope and context of a current session. Given the maturity of the numerous stand-alone JTA TransactionManager implementations, most, if not all, applications should be using JTA transaction management, whether or not they are deployed into a J2EE container. Based on that, the JTA-based contextual sessions are all you need to use.

(Hibernate Reference Documentation | Architecture. Contextual Sessions)

like image 541
EugeneP Avatar asked Mar 31 '10 11:03

EugeneP


People also ask

Does hibernate use JTA?

Hibernate uses JDBC connections and JTA resources directly, without adding any additional locking behavior. It is important for you to become familiar with the JDBC, ANSI SQL, and transaction isolation specifics of your database management system. Hibernate does not lock objects in memory.

Does Tomcat support JTA?

The answer is: NO. Tomcat 6. x (7&8) don't provide JTA out-of-the-box because they don't have a transaction manager which is required as a separate component to monitor multiple resources (e.g. datasources).

What is the difference between JTA and JPA?

JPA (Java Persistence API) is the Java ORM standard/specification for storing, accessing, and managing Java objects in a relational database. Hibernate is an implementation of the Java Persistence API (JPA) specification. JTA (Java Transaction API) is the Java standard/specification for distributed transactions.

How does JTA transaction work?

The JTA specifies standard Java interfaces between a transaction manager and the parties involved in a distributed transaction system: the application, the application server, and the resource manager that controls access to the shared resources affected by the transactions.


2 Answers

If you want JTA support in Tomcat you'll need to use a standalone transaction manager like Atomikos, JOTM, Bitronix, SimpleJTA, JBossTS or GeronimoTM/Jencks. But honestly, if you're not going to handle transactions across multiple resources, then you can live without JTA (and if you really need JTA, use a full blown application server).

like image 189
Pascal Thivent Avatar answered Oct 25 '22 02:10

Pascal Thivent


If you just want to use SessionFactory.getCurrentSession() you can just add the following two lines to your hibernate.cfg.xml:

<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> <property name="hibernate.current_session_context_class">thread</property> 

This will give you a unique Session for each thread. As a servlet request is always handled within one thread (given that your code doesn't spawn new ones), the Session will live for the whole request.

Don't forget to use a filter to close the Session after the request!

like image 23
FRotthowe Avatar answered Oct 25 '22 02:10

FRotthowe