This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
mock question: Given a stateless session bean with container-managed transaction demarcation, from which two methods can a developer access another enterprise bean? (Choose two.) A. bean constructor B. Timeout callback method C. PreDestroy lifecycle callback method D. PostConstruct lifecycle callback method E. business method from the business interface
I think we can do DI in any of these methods,but given answer is B,E,anyone please clear for me.
For me, the most important phrase to understand this part of the specs is in 4.5.2:
Accessing resource managers, enterprise beans, and the EntityManager is disallowed in the session bean methods for which the container does not have a meaningful transaction context and/or client security context.
A- An EJB Constructor doesn't have meaningful transaction context nor client security context. In his constructor, an EJB can't access anything.
B- The specs (18.2.5) says:
The timeout callback method is typically has transaction attribute REQUIRED or REQUIRES_NEW. If the transaction is rolled back, the container retries the timeout.
So it can access other EJB.
C and D- PostConstruct and PreDestroy of Stateful SB can access other EJB but the same lifecycle callback, when implemented on a Stateless SB, can't. I think it's because: - In the case of Stateful SB, the creation and destruction result from an action of the user. - In the case of Stateless SB, the creation and destruction result from a decision of the container (Pool Management).
E- A business method has meaningful transaction context or client security context.
Those two tables (in 4.4.1 and 4.5.2) are very important for the exam. But the understanding is not very easy.
Hello everyone, very helpful answer Benoît but i have some doubts about the spec int this matter. On the page 79 (JSR 220) there is a table describing "Operations Allowed in the Methods of a Stateful Session Bean". There are PostConstruct, Pre-Destroy, PrePassivate, PostActivate lifecycle methods on the left and on the right access to Resource manager, Enterprise bean and others described as allowed. Isn't it conflicting with:
Accessing resource managers and enterprise beans is disallowed in the session bean methods
for which the container does not have a meaningful transaction context and/or client security