Spring+Hibernate 4+Glassfish之使用JTA Transaction
今天下午开始尝试将项目的transaction交给Glassfish的JTA管理,因为之后会使用到JMS,需要与JDBC组成跨data source的事务。但是不知道是没人这么干过还是大家不屑于将完整的配置过程就下来,JBoss的官方文档、Spring的官方文档、SOF都没有可用的配置建议。经过差不多半天时间的Google和尝试,终于配置成功,在此分享
环境:
- Spring 3.1.1
- Hibernate 4.1.1
- Glassfish 3.1.2
关键配置在1和2处:
When configuring Hibernate’s transaction factory, choose
org.hibernate.transaction.JTATransactionFactoryif you use JTA directly (BMT), andorg.hibernate.transaction.CMTTransactionFactoryin a CMT session bean. Remember to also sethibernate.transaction.manager_lookup_class. Ensure that yourhibernate.current_session_context_classis either unset (backwards compatibility), or is set to"jta".⋯⋯
See the Javadocs for the
org.hibernate.context.spi.CurrentSessionContextinterface for a detailed discussion of its contract. It defines a single method,currentSession(), by which the implementation is responsible for tracking the current contextual session. Out-of-the-box, Hibernate comes with three implementations of this interface:
org.hibernate.context.internal.JTASessionContext: current sessions are tracked and scoped by aJTAtransaction. The processing here is exactly the same as in the older JTA-only approach. See the Javadocs for details.org.hibernate.context.internal.ThreadLocalSessionContext:current sessions are tracked by thread of execution. See the Javadocs for details.org.hibernate.context.internal.ManagedSessionContext: current sessions are tracked by thread of execution. However, you are responsible to bind and unbind aSessioninstance with static methods on this class: it does not open, flush, or close aSession.
即需要设置hibernate.current_session_context_class为jta,同时设置hibernate.transaction.manager_lookup_class。但是这里耽误了我半天的就是hibernate.transaction.manager_lookup_class的设置,这里只说“需要设置”,但是没有给出具体的值,虽然文档中给出了一个TransactionManagerLookup接口的实现类列表,但是其中没有Glassfish⋯⋯
又经过了N久的Google,在这里和这里找到了一个神奇的实现类:org.hibernate.transaction.SunONETransactionManagerLookup。根据Javadoc,此类实现了“for Sun ONE Application Server 7 and above”的查找策略,想想GF的前身即是Sun AS,应该可以使用。试了一下,终于work~
这里遗留的一个问题是,我并没有在Hibernate 4.1.1的源代码里找到org.hibernate.transaction.SunONETransactionManagerLookup(上面的Javadoc也是3.6的Javadoc),甚至整个org.hibernate.transaction包下面只有TransactionManagerLookup接口的定义。因此究竟是怎么work的我没有找到最终答案,如果有人知道,希望可以在这里留下答案
浙公网安备 33010602011771号