Skip to content

SpringBeanAutowiringInterceptor not closing ApplicationContext after EJB initialisation exception [SPR-10013] #14647

@spring-projects-issues

Description

@spring-projects-issues

Michael Rippon opened SPR-10013 and commented

SpringBeanAutowiringInterceptor by default uses SingletonBeanFactoryLocator, which keeps an internal counter of references to the SpringContext. This is incremented on @PostConstruct, and decremented on @PreDestroy, however, according to the EJB spec:

The following scenarios result in the PreDestroy lifecycle callback interceptor method(s) not being called for an instance:
...
• A system exception thrown from the instance’s method to the container.

This means that if an exception is thrown during EJB initialisation, the @PreDestroy method is never called, the reference count in SingletonBeanFactoryLocator is never decremented, and the application context is therefore never closed.

This has implications (for example), with the MBean exporter, where MBeans are never unregistered, and the next application deployment fails as the previous MBean instances already exist.

I suggest that in the case where an exception is thrown during the @PostConstruct phase, the beanFactoryReference should be released. Ie:

doAutowireBean(invocationContext.getTarget());
try {
invocationContext.proceed();
}
catch {
...
doReleaseBean(invocationContext.getTarget());
}


Affects: 3.1 GA

Referenced from: commits eb492ed, 721fa9d

Backported to: 3.1.4

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions