Implement CDI/Spring bean releasing more cleanly

Description

Currently, CDI beans are held in a map, and released on shutdown.
This is not ideal, as some beans may only be used during bootstrap, so we could release them earlier.

One way to solve this would be to make BridgeBuilder and BeanResolver return a "releasable" BeanHolder, so that ContainedBeans from CDI/Spring (obtained through Hibernate ORM) can be properly released.
This would also allow users to properly release their own CDI beans, for instance those built using an Instance<MyBridge> and returned by a custom BridgeBuilder: users would be able to implement a BeanHolder that calls instance.destroy( bean ) upon release.

Also, the fact that most of or beans extend AutoCloseable makes CDI lifecycle a bit confusing: should users add a @PreDestory method? Should they simply add code to #close()? Both?

I would personally be in favor of removing the close() method from user-provided components. I would make the reflection-based bean provider return a CloseableBeanReference whenever a user bean implements AutoCloseable. This reference would just call close() when release() is called, wrapping the exception with a RuntimeException if necessary.

DECISION on the striked-through text above: let's just improve the Javadoc to 1) tell in which order the close() / @PreDestroy-annotated methods will be called and 2) remind users that their close() methods should work correctly even if called twice.

Activity

Show:

Yoann Rodière August 22, 2018 at 2:20 PM

> Also, the fact that most of or beans extend AutoCloseable makes CDI lifecycle a bit confusing: should users add a @PreDestory method? Should they simply add code to #close()? Both?

DECISION: let's just improve the Javadoc to 1) tell in which order the close() / @PreDestroy-annotated methods will be called and 2) remind users that their close() methods should work correctly even if called twice.

Fixed

Details

Assignee

Reporter

Components

Sprint

Fix versions

Priority

Created May 23, 2018 at 2:06 PM
Updated January 31, 2019 at 3:08 PM
Resolved December 11, 2018 at 6:25 PM