22

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)

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
EugeneP
  • 11,783
  • 32
  • 96
  • 142
  • Ok, I need a JNDI configured SessionFactory. Now when I'm able to get a SessionFactory thru lookup I need to configure JTA. My home classes use getCurrentSession() and as I can read in documentation this works only in JTA configured environment. Besides, as I already mentioned, virtually everyone recommends using JTA in JEE environment. – EugeneP Mar 31 '10 at 12:02
  • 3
    Don't use JTA just because someone told you that you need it. JTA is very complex, and unnecessary in 99% of situations. Hibernate/JPA will work just fine without it. – skaffman Mar 31 '10 at 12:38
  • *Ok, I need a JNDI configured SessionFactory.* Why? – Pascal Thivent Mar 31 '10 at 12:58
  • @Pascal Thivent Because Hibernate developers in their documentation recommend doing so. – EugeneP Mar 31 '10 at 13:21
  • @EugeneP This is something I tend to use with a fully Java EE compliant server but, to be honest, I've never been able to find a good justification (the only one is when you run Hibernate as a JMX service). So I may be missing something but having the SessionFactory bound to a static (and final) variable in an HibernateUtil class is ok for me (and this is the recommended approach in Hibernate in Action for a servlet container). – Pascal Thivent Mar 31 '10 at 13:50

2 Answers2

33

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

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
6

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!

FRotthowe
  • 3,662
  • 25
  • 31
  • Be aware of the fact, that these parameters are obsoleted in Hibernate 5. For further information please check the [migration guide](https://github.com/hibernate/hibernate-orm/blob/5.0/migration-guide.adoc). Example code from our project: `hibernateProperties.setProperty("hibernate.transaction.jta.platform", "com.atomikos.icatch.jta.hibernate4.AtomikosPlatform");` `hibernateProperties.setProperty("hibernate.transaction.coordinator_class", "jta");` – Ursache Mar 22 '18 at 15:09