How to use JTA support in Tomcat 6 for Hibernate?

后端 未结 2 459
感动是毒
感动是毒 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:21

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

    org.hibernate.transaction.JDBCTransactionFactory
    thread
    

    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!

提交回复
热议问题