2

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?

2
  • please, provide some code of what are you testing, hopefully with comments about what are you expecting, and which AS are you using (just for testing purposes)? Commented Feb 19, 2014 at 21:57
  • I can post some code (once I have again access to my Java EE Environment) but the theme is quite theoretish. I want to know if a System Exception in @PostConstruct Method in a Singleton Bean causes the dirscarding of Bean by the Container. Exactly that case is described in the two 'quotes' cited in my question and from my point of view they are conflicting. Commented Feb 20, 2014 at 12:55

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what you stated in your answer I used to believe, but afterreading more sections of the specs, I started to doubt. Perhaps the non-destruction of the Singleton by a System Exception in a lifecycle callback method applies not to 'PostConstruct' method, thus @PreDestroy.
If @PostConstruct throws a runtimeException, the singleton wont be created. Also, @PostContruct runs only once in the lifecycle of the ejb.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.