11/*
2- * Copyright 2002-2013 the original author or authors.
2+ * Copyright 2002-2014 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1717package org .springframework .orm .hibernate4 ;
1818
1919import java .lang .reflect .Method ;
20+ import javax .transaction .Status ;
21+ import javax .transaction .SystemException ;
22+ import javax .transaction .TransactionManager ;
2023
24+ import org .apache .commons .logging .LogFactory ;
2125import org .hibernate .FlushMode ;
2226import org .hibernate .HibernateException ;
2327import org .hibernate .Session ;
@@ -44,6 +48,8 @@ public class SpringSessionContext implements CurrentSessionContext {
4448
4549 private final SessionFactoryImplementor sessionFactory ;
4650
51+ private TransactionManager transactionManager ;
52+
4753 private CurrentSessionContext jtaSessionContext ;
4854
4955
@@ -56,13 +62,14 @@ public SpringSessionContext(SessionFactoryImplementor sessionFactory) {
5662 try {
5763 Object jtaPlatform = sessionFactory .getServiceRegistry ().getService (ConfigurableJtaPlatform .jtaPlatformClass );
5864 Method rtmMethod = ConfigurableJtaPlatform .jtaPlatformClass .getMethod ("retrieveTransactionManager" );
59- Object transactionManager = ReflectionUtils .invokeMethod (rtmMethod , jtaPlatform );
60- if (transactionManager != null ) {
65+ this . transactionManager = ( TransactionManager ) ReflectionUtils .invokeMethod (rtmMethod , jtaPlatform );
66+ if (this . transactionManager != null ) {
6167 this .jtaSessionContext = new SpringJtaSessionContext (sessionFactory );
6268 }
6369 }
6470 catch (Exception ex ) {
65- throw new IllegalStateException ("Could not introspect Hibernate JtaPlatform for SpringJtaSessionContext" , ex );
71+ LogFactory .getLog (SpringSessionContext .class ).warn (
72+ "Could not introspect Hibernate JtaPlatform for SpringJtaSessionContext" , ex );
6673 }
6774 }
6875
@@ -82,7 +89,7 @@ else if (value instanceof SessionHolder) {
8289 if (TransactionSynchronizationManager .isSynchronizationActive () &&
8390 !sessionHolder .isSynchronizedWithTransaction ()) {
8491 TransactionSynchronizationManager .registerSynchronization (
85- new SpringSessionSynchronization (sessionHolder , this .sessionFactory ));
92+ new SpringSessionSynchronization (sessionHolder , this .sessionFactory , false ));
8693 sessionHolder .setSynchronizedWithTransaction (true );
8794 // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
8895 // with FlushMode.MANUAL, which needs to allow flushing within the transaction.
@@ -95,15 +102,36 @@ else if (value instanceof SessionHolder) {
95102 }
96103 return session ;
97104 }
98- else if (this .jtaSessionContext != null ) {
99- Session session = this .jtaSessionContext .currentSession ();
100- if (TransactionSynchronizationManager .isSynchronizationActive ()) {
101- TransactionSynchronizationManager .registerSynchronization (new SpringFlushSynchronization (session ));
105+
106+ if (this .transactionManager != null ) {
107+ try {
108+ if (this .transactionManager .getStatus () == Status .STATUS_ACTIVE ) {
109+ Session session = this .jtaSessionContext .currentSession ();
110+ if (TransactionSynchronizationManager .isSynchronizationActive ()) {
111+ TransactionSynchronizationManager .registerSynchronization (new SpringFlushSynchronization (session ));
112+ }
113+ return session ;
114+ }
115+ }
116+ catch (SystemException ex ) {
117+ throw new HibernateException ("JTA TransactionManager found but status check failed" , ex );
118+ }
119+ }
120+
121+ if (TransactionSynchronizationManager .isSynchronizationActive ()) {
122+ Session session = this .sessionFactory .openSession ();
123+ if (TransactionSynchronizationManager .isCurrentTransactionReadOnly ()) {
124+ session .setFlushMode (FlushMode .MANUAL );
102125 }
126+ SessionHolder sessionHolder = new SessionHolder (session );
127+ TransactionSynchronizationManager .registerSynchronization (
128+ new SpringSessionSynchronization (sessionHolder , this .sessionFactory , true ));
129+ TransactionSynchronizationManager .bindResource (this .sessionFactory , sessionHolder );
130+ sessionHolder .setSynchronizedWithTransaction (true );
103131 return session ;
104132 }
105133 else {
106- throw new HibernateException ("No Session found for current thread" );
134+ throw new HibernateException ("Could not obtain transaction-synchronized Session for current thread" );
107135 }
108136 }
109137
0 commit comments