Skip to content

Commit 439b775

Browse files
committed
fixed "hibernateManagedSession" mode to actually work against Hibernate 4.0 (SPR-8776)
1 parent 48836e2 commit 439b775

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.orm.hibernate4;
1818

19+
import java.lang.reflect.Method;
1920
import java.sql.Connection;
2021
import javax.sql.DataSource;
2122

@@ -44,6 +45,8 @@
4445
import org.springframework.transaction.support.DefaultTransactionStatus;
4546
import org.springframework.transaction.support.ResourceTransactionManager;
4647
import org.springframework.transaction.support.TransactionSynchronizationManager;
48+
import org.springframework.util.ClassUtils;
49+
import org.springframework.util.ReflectionUtils;
4750

4851
/**
4952
* {@link org.springframework.transaction.PlatformTransactionManager}
@@ -102,6 +105,15 @@
102105
public class HibernateTransactionManager extends AbstractPlatformTransactionManager
103106
implements ResourceTransactionManager, InitializingBean {
104107

108+
/**
109+
* A Method handle for the <code>SessionFactory.getCurrentSession()</code> method.
110+
* The return value differs between Hibernate 3.x and 4.x; for cross-compilation purposes,
111+
* we have to use reflection here as long as we keep compiling against Hibernate 3.x jars.
112+
*/
113+
private static final Method getCurrentSessionMethod =
114+
ClassUtils.getMethod(SessionFactory.class, "getCurrentSession");
115+
116+
105117
private SessionFactory sessionFactory;
106118

107119
private DataSource dataSource;
@@ -281,7 +293,7 @@ protected Object doGetTransaction() {
281293
}
282294
else if (this.hibernateManagedSession) {
283295
try {
284-
Session session = getSessionFactory().getCurrentSession();
296+
Session session = (Session) ReflectionUtils.invokeMethod(getCurrentSessionMethod, this.sessionFactory);
285297
if (logger.isDebugEnabled()) {
286298
logger.debug("Found Hibernate-managed Session [" + session + "] for Spring-managed transaction");
287299
}

0 commit comments

Comments
 (0)