You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support automatic detection of close methods [SPR-8114] #12768
When using Spring's FactoryBean types such as LocalSessionFactoryBean destruction of the actual bean (the Hibernate Session in the case of LSFB) is usually handled automatically by the FactoryBean.
However, when using Spring's new *Builder APIs within @Bean methods, no FactoryBean-like object is registered with the container. This means that it is up to the user to remember to configure the destroy-method attribute of the @Bean annotation, which is likely to be forgotten.
To remedy this situation, Spring should automatically detect common close/destruction method names and signatures and automatically invoke them at container shutdown time.
Obviously, Spring's DisposableBean is already supported in this way, as are methods annotated with @PreDestroy. When dealing with third-party types, however, the user is not at liberty to add these interfaces/annotations. In the case of Hibernate's Session interface, the method to be called is
public Connection close() throws HibernateException;
As this method is not annotated in any way nor does it implement any standard interface with destruction semantics, we can only assume based on it's name that it is to be invoked.
A different approach has been taken that the original suggestions in the issue description, see commit comment below. This approach means that there is no guesswork by the container to figure out whether and which close() methods are present, but does require that (a) we carefully implement this proxying for each *Builder type (SessionFactoryBuilder and AnnotationSessionFactoryBuilder in this case), and (b) users will continue to have to set @Bean(destroyMethod="close") for objects that they manage directly.
Automatically close SessionFactory objects
SessionFactory objects created by
SessionFactoryBuilderSupport#buildSessionFactory are now DisposableBean
proxies that call SessionFactory#close and release any threadlocal
DataSource object.
This is the same behavior that has always occurred during LSFBean and
ASFBean destruction lifecycles (and still does). This destruction logic
has now been factored out into
SessionFactoryBuilderSupport#closeHibernateSessionFactory such that all
SFB types can reuse it easily.
Note that LSFBean and ASFBean are subclasses, respectively, of SFBuilder
and ASFBuilder and they each must disable the DisposableBean proxying in
order to avoid duplicate attempts at closing the SessionFactory. See
the implementations of wrapSessionFactoryIfNeccesary() for details.
Issue: SPR-8114
Activity
spring-projects-issues commentedon Apr 26, 2011
Chris Beams commented
A different approach has been taken that the original suggestions in the issue description, see commit comment below. This approach means that there is no guesswork by the container to figure out whether and which close() methods are present, but does require that (a) we carefully implement this proxying for each
*Buildertype (SessionFactoryBuilderandAnnotationSessionFactoryBuilderin this case), and (b) users will continue to have to set@Bean(destroyMethod="close") for objects that they manage directly.