Auto-unwrap SessionFactoryBuilder-created proxies

SessionFactory proxies created by (Annotation)SessionFactoryBuilder now
implement InfrastructureProxy to ensure they are automatically unwrapped
for transaction resource management purposes.

Issue: SPR-8492
This commit is contained in:
Chris Beams 2011-07-30 18:05:34 +00:00
parent 35d2ab3bf9
commit 9f4a46e24c
2 changed files with 7 additions and 0 deletions

View File

@ -42,6 +42,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.ImportResource;
import org.springframework.core.InfrastructureProxy;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.dao.support.PersistenceExceptionTranslator; import org.springframework.dao.support.PersistenceExceptionTranslator;
@ -137,6 +138,7 @@ public class HibernateSessionFactoryConfigurationTests {
SessionFactory sessionFactory = ctx.getBean(SessionFactory.class); SessionFactory sessionFactory = ctx.getBean(SessionFactory.class);
assertThat(sessionFactory, instanceOf(DisposableBean.class)); assertThat(sessionFactory, instanceOf(DisposableBean.class));
assertThat(sessionFactory, instanceOf(SessionFactoryImplementor.class)); assertThat(sessionFactory, instanceOf(SessionFactoryImplementor.class));
assertThat(sessionFactory, instanceOf(InfrastructureProxy.class));
assertThat(sessionFactory.toString(), startsWith("DisposableBean proxy for SessionFactory")); assertThat(sessionFactory.toString(), startsWith("DisposableBean proxy for SessionFactory"));
ctx.close(); ctx.close();
assertTrue("SessionFactory was not closed as expected", sessionFactory.isClosed()); assertTrue("SessionFactory was not closed as expected", sessionFactory.isClosed());

View File

@ -52,6 +52,7 @@ import org.hibernate.transaction.JTATransactionFactory;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.InfrastructureProxy;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
@ -566,6 +567,7 @@ public abstract class SessionFactoryBuilderSupport<This extends SessionFactoryBu
new Class<?>[] { new Class<?>[] {
SessionFactory.class, SessionFactory.class,
SessionFactoryImplementor.class, SessionFactoryImplementor.class,
InfrastructureProxy.class,
DisposableBean.class DisposableBean.class
}, },
new InvocationHandler() { new InvocationHandler() {
@ -573,6 +575,9 @@ public abstract class SessionFactoryBuilderSupport<This extends SessionFactoryBu
if (ReflectionUtils.isToStringMethod(method)) { if (ReflectionUtils.isToStringMethod(method)) {
return String.format("DisposableBean proxy for SessionFactory [%s]", rawSf.toString()); return String.format("DisposableBean proxy for SessionFactory [%s]", rawSf.toString());
} }
if (method.equals(InfrastructureProxy.class.getMethod("getWrappedObject"))) {
return rawSf;
}
if (method.equals(DisposableBean.class.getMethod("destroy"))) { if (method.equals(DisposableBean.class.getMethod("destroy"))) {
closeHibernateSessionFactory(SessionFactoryBuilderSupport.this, rawSf); closeHibernateSessionFactory(SessionFactoryBuilderSupport.this, rawSf);
rawSf.close(); rawSf.close();