How to use JTA support in Tomcat 6 for Hibernate?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 12:09: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)


回答1:


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




回答2:


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!



来源:https://stackoverflow.com/questions/2552612/how-to-use-jta-support-in-tomcat-6-for-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!