Skip to content

Commit 7d39957

Browse files
committed
SessionFactoryUtils defensively accesses getProperties method via reflection
Issue: SPR-14365
1 parent 9001af7 commit 7d39957

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/SessionFactoryUtils.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.orm.hibernate5;
1818

1919
import java.lang.reflect.Method;
20+
import java.util.Map;
2021
import javax.sql.DataSource;
2122

2223
import org.apache.commons.logging.Log;
@@ -62,6 +63,7 @@
6263
import org.springframework.dao.PessimisticLockingFailureException;
6364
import org.springframework.jdbc.datasource.DataSourceUtils;
6465
import org.springframework.util.Assert;
66+
import org.springframework.util.ClassUtils;
6567
import org.springframework.util.ReflectionUtils;
6668

6769
/**
@@ -148,12 +150,16 @@ public static void closeSession(Session session) {
148150
* @see ConnectionProvider
149151
*/
150152
public static DataSource getDataSource(SessionFactory sessionFactory) {
151-
if (sessionFactory instanceof SessionFactoryImplementor) {
152-
SessionFactoryImplementor sfi = (SessionFactoryImplementor) sessionFactory;
153-
Object dataSourceValue = sfi.getProperties().get(Environment.DATASOURCE);
153+
Method getProperties = ClassUtils.getMethodIfAvailable(sessionFactory.getClass(), "getProperties");
154+
if (getProperties != null) {
155+
Map<?, ?> props = (Map<?, ?>) ReflectionUtils.invokeMethod(getProperties, sessionFactory);
156+
Object dataSourceValue = props.get(Environment.DATASOURCE);
154157
if (dataSourceValue instanceof DataSource) {
155158
return (DataSource) dataSourceValue;
156159
}
160+
}
161+
if (sessionFactory instanceof SessionFactoryImplementor) {
162+
SessionFactoryImplementor sfi = (SessionFactoryImplementor) sessionFactory;
157163
try {
158164
ConnectionProvider cp = sfi.getServiceRegistry().getService(ConnectionProvider.class);
159165
if (cp != null) {

0 commit comments

Comments
 (0)