Implement SessionFactoryImplementor in SF proxies

SessionFactoryBuilderSupport implementations create DisposableBean
proxies for SessionFactory objects created using #buildSessionFactory.

Prior to this change, these proxies create problems when working agaist
SessionFactoryUtils.getDataSource(SessionFactory), because this method
expects the given SessionFactory to implement Hibernate's
SessionFactoryImplementor interface (which the stock SessionFactoryImpl
does).

With this change, the DisposableBean proxies created by SFBuilders
now also implement SessionFactoryImplementor to satisfy this and
probably other such cases.

Issue: SPR-8469

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4683 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Chris Beams 2011-07-07 19:42:07 +00:00
parent c3affadc55
commit 08ccd19c76
2 changed files with 13 additions and 7 deletions

View File

@ -33,6 +33,7 @@ import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
import org.hibernate.engine.SessionFactoryImplementor;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
@ -131,10 +132,11 @@ public class HibernateSessionFactoryConfigurationTests {
}
@Test
public void builtSessionFactoryIsDisposableBeanProxy() {
public void builtSessionFactoryIsProxyImplementingDisposableBeanAndSessionFactoryImplementor() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AnnotationSessionFactoryConfig.class);
SessionFactory sessionFactory = ctx.getBean(SessionFactory.class);
assertThat(sessionFactory, instanceOf(DisposableBean.class));
assertThat(sessionFactory, instanceOf(SessionFactoryImplementor.class));
assertThat(sessionFactory.toString(), startsWith("DisposableBean proxy for SessionFactory"));
ctx.close();
assertTrue("SessionFactory was not closed as expected", sessionFactory.isClosed());

View File

@ -232,8 +232,9 @@ public abstract class SessionFactoryBuilderSupport<This extends SessionFactoryBu
/**
* Build the underlying Hibernate SessionFactory.
* @return the raw SessionFactory (potentially to be wrapped with a
* transaction-aware proxy before it is exposed to the application)
* @return the {@code SessionFactory}, potentially wrapped as a
* {@code DisposableBean} and/or transaction-aware proxy before it is exposed to the
* application.
* @throws Exception in case of initialization failure
*/
public SessionFactory buildSessionFactory() throws Exception {
@ -244,8 +245,9 @@ public abstract class SessionFactoryBuilderSupport<This extends SessionFactoryBu
/**
* Populate the underlying {@code Configuration} instance with the various
* properties of this builder. Customization may be performed through
* {@code pre*} and {@code post*} methods.
* properties of this builder, then return the raw session factory resulting from
* calling {@link Configuration#buildSessionFactory()}. Customization may be performed
* through {@code pre*} and {@code post*} methods.
* @see #preBuildSessionFactory()
* @see #postProcessMappings()
* @see #postBuildSessionFactory()
@ -565,8 +567,9 @@ public abstract class SessionFactoryBuilderSupport<This extends SessionFactoryBu
* <p>Subclasses may override this to implement transaction awareness through
* a {@code SessionFactory} proxy for example, or even to avoid creation of the
* {@code DisposableBean} proxy altogether.
* @param rawSf the raw {@code SessionFactory} as built by {@link #buildSessionFactory()}
* @return the {@code SessionFactory} reference to expose
* @param rawSf the raw {@code SessionFactory} as built by {@link #doBuildSessionFactory()}
* @return a proxied {@code SessionFactory} if wrapping was necessary, otherwise the
* original given 'raw' {@code SessionFactory} object.
* @see #buildSessionFactory()
*/
protected SessionFactory wrapSessionFactoryIfNecessary(final SessionFactory rawSf) {
@ -574,6 +577,7 @@ public abstract class SessionFactoryBuilderSupport<This extends SessionFactoryBu
this.beanClassLoader,
new Class<?>[] {
SessionFactory.class,
SessionFactoryImplementor.class,
DisposableBean.class
},
new InvocationHandler() {