my dog learned polymorphism
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes ETS Question doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "ETS Question doubts" Watch "ETS Question doubts" New topic

ETS Question doubts

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
ETS Question

For a stateless session bean
Given the below code

a) It can be a business method
b) It can be a @PostConstruct method
c) It can be a @Remove method
d) This code is invalid in any stateless session bean
e) It may be DI method such as setSessionContext.

Answer: It can be a business method.

I invoked the lookup() from PostConstruct, PreDestroy and business method and it worked fine. This means that above method
can be any one of PostConstruct, PreDestroy and business method and not just business method. Please clarify.
ETS Answer: A stateless session bean cannot access resource managers in @PostConstruct methods.

12:18:12,796 INFO [STDOUT] Start:TestBeanIC.init()
12:18:12,796 INFO [STDOUT] Start:TestBeanIC.lookup()
12:18:12,890 INFO [STDOUT] Exception:javax.naming.NameNotFoundException: jms not bound
12:18:12,890 INFO [STDOUT] End:TestBeanIC.lookup()
12:18:12,890 INFO [STDOUT] End:TestBeanIC.init()
12:18:13,062 INFO [STDOUT] Start:TestBeanIC.method1()
12:18:13,062 INFO [STDOUT] Start:TestBeanIC.lookup()
12:18:13,062 INFO [STDOUT] Exception:javax.naming.NameNotFoundException: jms not bound
12:18:13,062 INFO [STDOUT] End:TestBeanIC.lookup()
12:18:13,062 INFO [STDOUT] End:TestBeanIC.method1()
12:18:13,156 INFO [STDOUT] Start:TestBeanIC.method2()
12:18:13,156 INFO [STDOUT] Start:TestBeanIC.lookup()
12:18:13,156 INFO [STDOUT] Exception:javax.naming.NameNotFoundException: jms not bound
12:18:13,156 INFO [STDOUT] End:TestBeanIC.lookup()
12:18:13,156 INFO [STDOUT] End:TestBeanIC.method2()
12:21:44,750 INFO [STDOUT] Start:TestBeanIC.destroy()
12:21:44,750 INFO [STDOUT] Start:TestBeanIC.lookup()
12:21:44,765 INFO [STDOUT] Exception:javax.naming.NameNotFoundException: env not bound
12:21:44,765 INFO [STDOUT] End:TestBeanIC.lookup()
12:21:44,765 INFO [STDOUT] End:TestBeanIC.destroy()
12:21:44,765 INFO [EJBContainer] STOPPED EJB: com.ejb3inaction.actionbazaar.test.TestBeanIC ejbName: TestBeanIC

A related question:
Is this statement correct?

Q) All Session beans can retreive resource manager references in ejbCreate().
Answer: Only stateful session beans can do so in their ejbCreate(). Stateless session
beans can do so only in their business methods becuase ejbCreate() and ejbRemove()
do not have any meaningful client transaction context and security context.

My questions:
a) What is ejbCreate() ? How and when is it invoked for stateless and stateful session bean? Is this from EJB 2.1
b) What do you mean by retreive resource manager references?

Can someone explain me with some code.

LakshmiNarayana Kodali

Joined: Oct 30, 2008
Posts: 16
For the Question 1 which you posted regarding lookup() method,

the answer is a,b,c.

In the lookup() method, 2 components are retrieved.
1. JMS resource using JNDI lookup (i,e Resource access)
2. Enterprise Bean access.

Both of these 2 things can be done using business method and also lifecycle callback methods.

So lookup() can be
a) It can be a business method
b) It can be a @PostConstruct method (It is lifecycle callback method)
c) It can be a @Remove method (It is also a business method in Stateful Bean)

These are the following things which can be done using both business methods and lifecycle callback methods.

1. SessionContext methods: getBusinessObject, getEJBHome, getEJBLocalHome, getCallerPrincipal, isCallerInRole, getEJBObject, getEJBLocalObject,
2. JNDI access to java:comp/env
3. Resource manager access
4. Enterprise bean access
5. EntityManagerFactory access
6. EntityManager access

LakshmiNarayana Kodali
SCJP 5(98%), SCWCD(100%), SCBCD in progress
I agree. Here's the link:
subject: ETS Question doubts
It's not a secret anymore!