Reputation: 9232
I am reading the EJB specification
and I got confused by a contradiction from my point of view and particularly regarding the way the Container
behaves when a System Exception
is thrown in lifecycle callback method of Singleton Bean
.
Section 12.3.1 :
A runtime exception thrown by any lifecycle interceptor callback method causes the bean instance and its interceptors to be discarded after the interceptor chain unwindsce and any associated interceptor instances are discarded (except for Singleton Beans).
Section 4.8.4 :
Errors occurring during Singleton initialization are considered fatal and must result in the discarding of the Singleton instance. Possible initialization errors include injection failure, a system exception thrown from a PostConstruct method, or the failure of a PostConstruct method container-managed transaction to successfully commit.
Unlike instances of other component types, system exceptions thrown from business methods or callbacks of a Singleton do not result in the destruction of the Singleton instance.
So, what is true according the Section 4.8.4? If a System Exception
is thrown in a @PostConstruct
method or the Interceptor
@PostConstruct
method is the Bean
discarded? I have tried it throwing an EJBException
and the result was that the Beans initialization failed.
More surprising was the fact, that when throwing an EJBException
from a Singleton Bean
Business Method, the client received the Exception
and no other method was further executed. Can I assume that the Bean Instance was discarded? According to the Specification that should not have caused the destruction of the Bean Instance
. What is true in both cases?
Upvotes: 2
Views: 1601
Reputation: 3387
I was asking for code, because is maybe a vendor bug. Just as you read, if there is an EjbException at @PostContruct time, the singleton is not builded. After that, a singleton method could throw anykind of exception (checked or unchecked) and the instance should be still running.
The interceptor lifecycle is attached to the singleton lifecycle, so, if the interceptor cant executed its @PostContruct, the singleton will no be created.
Other thing to keep in mind is : If an EJB (@Stateless or @Stateful) throws an EjbException (or its interceptors), automatically the instance gets discarded, because of that, there is a note in the spec that says: [58] Except for singletons. See Section 4.8.4
Upvotes: 1