Skip to content

Commit 1a8531b

Browse files
committed
Revert "Automatically close SessionFactory objects"
This reverts commit bb4f48d. Issue: SPR-8114
1 parent b82d5ae commit 1a8531b

File tree

6 files changed

+28
-94
lines changed

6 files changed

+28
-94
lines changed

org.springframework.integration-tests/src/test/java/org/springframework/orm/hibernate3/HibernateSessionFactoryConfigurationTests.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
import static org.hamcrest.CoreMatchers.instanceOf;
2020
import static org.hamcrest.CoreMatchers.is;
2121
import static org.hamcrest.CoreMatchers.notNullValue;
22-
import static org.hamcrest.Matchers.startsWith;
2322
import static org.junit.Assert.assertThat;
24-
import static org.junit.Assert.assertTrue;
2523

2624
import java.io.File;
2725
import java.util.List;
@@ -36,7 +34,6 @@
3634
import org.junit.Ignore;
3735
import org.junit.Test;
3836
import org.springframework.beans.factory.BeanCreationException;
39-
import org.springframework.beans.factory.DisposableBean;
4037
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4138
import org.springframework.context.annotation.Bean;
4239
import org.springframework.context.annotation.Configuration;
@@ -130,16 +127,6 @@ public void usingSessionFactoryBuilder_withLateCustomConfigurationClass() throws
130127
}
131128
}
132129

133-
@Test
134-
public void builtSessionFactoryIsDisposableBeanProxy() {
135-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AnnotationSessionFactoryConfig.class);
136-
SessionFactory sessionFactory = ctx.getBean(SessionFactory.class);
137-
assertThat(sessionFactory, instanceOf(DisposableBean.class));
138-
assertThat(sessionFactory.toString(), startsWith("DisposableBean proxy for SessionFactory"));
139-
ctx.close();
140-
assertTrue("SessionFactory was not closed as expected", sessionFactory.isClosed());
141-
}
142-
143130

144131
private void saveAndRetriveEntity(Class<?> configClass) {
145132
SessionFactory sessionFactory = new AnnotationConfigApplicationContext(configClass).getBean(SessionFactory.class);

org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/LocalSessionFactoryBean.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package org.springframework.orm.hibernate3;
1818

1919
import org.hibernate.HibernateException;
20-
import org.hibernate.SessionFactory;
2120

21+
import org.hibernate.SessionFactory;
2222
import org.springframework.dao.DataAccessException;
2323
import org.springframework.jdbc.support.SQLExceptionTranslator;
2424

@@ -110,11 +110,6 @@ public void setPersistenceExceptionTranslator(
110110
delegate.setPersistenceExceptionTranslator(hibernateExceptionTranslator);
111111
}
112112

113-
@Override
114-
public SessionFactory wrapSessionFactoryIfNecessary(SessionFactory rawSf) {
115-
return delegate.wrapSessionFactoryIfNecessary(rawSf);
116-
}
117-
118113
/**
119114
* @deprecated as of Spring 3.1 in favor of {@link #newSessionFactory()} which
120115
* can access the internal {@code Configuration} instance via {@link #getConfiguration()}.

org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/SessionFactoryBeanDelegate.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.orm.hibernate3;
1818

19+
import javax.sql.DataSource;
20+
1921
import org.hibernate.HibernateException;
2022
import org.hibernate.SessionFactory;
2123
import org.hibernate.cache.RegionFactory;
@@ -113,7 +115,23 @@ public void afterPropertiesSet() throws Exception {
113115
}
114116

115117
public void destroy() throws HibernateException {
116-
SessionFactoryBuilderSupport.closeHibernateSessionFactory(this.builder, this.sessionFactory);
118+
builder.logger.info("Closing Hibernate SessionFactory");
119+
DataSource dataSource = builder.getDataSource();
120+
if (dataSource != null) {
121+
// Make given DataSource available for potential SchemaExport,
122+
// which unfortunately reinstantiates a ConnectionProvider.
123+
SessionFactoryBuilderSupport.configTimeDataSourceHolder.set(dataSource);
124+
}
125+
try {
126+
builder.beforeSessionFactoryDestruction();
127+
}
128+
finally {
129+
this.sessionFactory.close();
130+
if (dataSource != null) {
131+
// Reset DataSource holder.
132+
SessionFactoryBuilderSupport.configTimeDataSourceHolder.remove();
133+
}
134+
}
117135
}
118136

119137
public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) {
@@ -124,11 +142,6 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
124142
return hibernateExceptionTranslator.translateExceptionIfPossible(ex);
125143
}
126144

127-
public SessionFactory wrapSessionFactoryIfNecessary(SessionFactory rawSf) {
128-
return rawSf;
129-
}
130-
131-
132145
/**
133146
* @see SessionFactoryBuilderSupport#preBuildSessionFactory()
134147
*/

org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/SessionFactoryBeanOperations.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,4 @@ public interface SessionFactoryBeanOperations
125125
*/
126126
DataAccessException translateExceptionIfPossible(RuntimeException ex);
127127

128-
/**
129-
* Override the default {@link DisposableBean} proxying behavior in
130-
* {@link SessionFactoryBuilderSupport#wrapSessionFactoryIfNecessary(SessionFactory)}
131-
* and return the raw {@code SessionFactory} instance, as {@link SessionFactory#close()}
132-
* will be called during this FactoryBean's normal {@linkplain #destroy() destruction lifecycle}.
133-
*/
134-
SessionFactory wrapSessionFactoryIfNecessary(SessionFactory rawSf);
135-
136128
}

org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/SessionFactoryBuilderSupport.java

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818

1919
import java.io.File;
2020
import java.lang.reflect.Array;
21-
import java.lang.reflect.InvocationHandler;
2221
import java.lang.reflect.Method;
23-
import java.lang.reflect.Proxy;
2422
import java.sql.Connection;
2523
import java.sql.SQLException;
2624
import java.sql.Statement;
@@ -34,7 +32,6 @@
3432

3533
import org.apache.commons.logging.Log;
3634
import org.apache.commons.logging.LogFactory;
37-
3835
import org.hibernate.HibernateException;
3936
import org.hibernate.Interceptor;
4037
import org.hibernate.Session;
@@ -48,10 +45,7 @@
4845
import org.hibernate.event.EventListeners;
4946
import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
5047
import org.hibernate.transaction.JTATransactionFactory;
51-
5248
import org.springframework.beans.BeanUtils;
53-
import org.springframework.beans.factory.DisposableBean;
54-
import org.springframework.context.ConfigurableApplicationContext;
5549
import org.springframework.core.io.ClassPathResource;
5650
import org.springframework.core.io.Resource;
5751
import org.springframework.dao.DataAccessException;
@@ -546,37 +540,16 @@ protected void postBuildSessionFactory() {
546540
}
547541

548542
/**
549-
* Wrap the given {@code SessionFactory} with a proxy, if demanded.
550-
* <p>The default implementation wraps the given {@code SessionFactory} as a Spring
551-
* {@link DisposableBean} proxy in order to call {@link SessionFactory#close()} on
552-
* {@code ApplicationContext} {@linkplain ConfigurableApplicationContext#close() shutdown}.
553-
* <p>Subclasses may override this to implement transaction awareness through
554-
* a {@code SessionFactory} proxy for example, or even to avoid creation of the
555-
* {@code DisposableBean} proxy altogether.
556-
* @param rawSf the raw {@code SessionFactory} as built by {@link #buildSessionFactory()}
557-
* @return the {@code SessionFactory} reference to expose
543+
* Wrap the given SessionFactory with a proxy, if demanded.
544+
* <p>The default implementation simply returns the given SessionFactory as-is.
545+
* Subclasses may override this to implement transaction awareness through
546+
* a SessionFactory proxy, for example.
547+
* @param rawSf the raw SessionFactory as built by {@link #buildSessionFactory()}
548+
* @return the SessionFactory reference to expose
558549
* @see #buildSessionFactory()
559550
*/
560-
protected SessionFactory wrapSessionFactoryIfNecessary(final SessionFactory rawSf) {
561-
return (SessionFactory) Proxy.newProxyInstance(
562-
this.beanClassLoader,
563-
new Class<?>[] {
564-
SessionFactory.class,
565-
DisposableBean.class
566-
},
567-
new InvocationHandler() {
568-
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
569-
if (ReflectionUtils.isToStringMethod(method)) {
570-
return String.format("DisposableBean proxy for SessionFactory [%s]", rawSf.toString());
571-
}
572-
if (method.equals(DisposableBean.class.getMethod("destroy"))) {
573-
closeHibernateSessionFactory(SessionFactoryBuilderSupport.this, rawSf);
574-
rawSf.close();
575-
return null;
576-
}
577-
return method.invoke(rawSf, args);
578-
}
579-
});
551+
protected SessionFactory wrapSessionFactoryIfNecessary(SessionFactory rawSf) {
552+
return rawSf;
580553
}
581554

582555
/**
@@ -1424,24 +1397,4 @@ public static LobHandler getConfigTimeLobHandler() {
14241397
return configTimeLobHandlerHolder.get();
14251398
}
14261399

1427-
static void closeHibernateSessionFactory(SessionFactoryBuilderSupport<?> builder, SessionFactory sessionFactory) {
1428-
builder.logger.info("Closing Hibernate SessionFactory");
1429-
DataSource dataSource = builder.getDataSource();
1430-
if (dataSource != null) {
1431-
// Make given DataSource available for potential SchemaExport,
1432-
// which unfortunately reinstantiates a ConnectionProvider.
1433-
SessionFactoryBuilderSupport.configTimeDataSourceHolder.set(dataSource);
1434-
}
1435-
try {
1436-
builder.beforeSessionFactoryDestruction();
1437-
}
1438-
finally {
1439-
sessionFactory.close();
1440-
if (dataSource != null) {
1441-
// Reset DataSource holder.
1442-
SessionFactoryBuilderSupport.configTimeDataSourceHolder.remove();
1443-
}
1444-
}
1445-
}
1446-
14471400
}

org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.hibernate.HibernateException;
2020
import org.hibernate.SessionFactory;
21-
2221
import org.springframework.context.ResourceLoaderAware;
2322
import org.springframework.core.io.ResourceLoader;
2423
import org.springframework.core.io.support.ResourcePatternUtils;
@@ -135,11 +134,6 @@ public void setPersistenceExceptionTranslator(
135134
delegate.setPersistenceExceptionTranslator(hibernateExceptionTranslator);
136135
}
137136

138-
@Override
139-
public SessionFactory wrapSessionFactoryIfNecessary(SessionFactory rawSf) {
140-
return delegate.wrapSessionFactoryIfNecessary(rawSf);
141-
}
142-
143137
/**
144138
* @deprecated as of Spring 3.1 in favor of {@link #scanPackages()} which
145139
* can access the internal {@code AnnotationConfiguration} instance via

0 commit comments

Comments
 (0)