Skip to content

Support automatic detection of close methods [SPR-8114] #12768

Closed

Description

@spring-projects-issues

Chris Beams opened SPR-8114 and commented

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.


Attachments:

Issue Links:

Referenced from: commits 5c27a04, 1a8531b

Activity

  1. spring-projects-issues commented on Apr 26, 2011

    @spring-projects-issues
    CollaboratorAuthor

    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 *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.

    Author: Chris Beams [email protected]
    Date: Tue Apr 26 17:46:14 2011 +0800

    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
    
  2. added this to the 3.1 M2 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions