How to use JTA support in Tomcat 6 for Hibernate?

后端 未结 2 457
感动是毒
感动是毒 2020-12-01 01:25

They recommend using JTA transaction support in Java EE environment.

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

相关标签:
2条回答
  • 2020-12-01 02:20

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

    0 讨论(0)
  • 2020-12-01 02:21

    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!

    0 讨论(0)
提交回复
热议问题