File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes operation allowed in session and entity beans 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 "operation allowed in session and entity beans" Watch "operation allowed in session and entity beans" New topic
Author

operation allowed in session and entity beans

Giju George
Ranch Hand

Joined: Jun 08, 2004
Posts: 333
A stateless session bean can call the EJBContext.getUserTx() in the ejbCreate() and ejbRemove(). But, then why can't it NOT invoke the user tx methods there ???

Also

In a stateful session bean's ejbActivate and ejbPassivate, we can call getCallerPrincipal() and isCallerInRole().. where as we cannot do the same in entity bean's ejbActivate and ejbPassivate...........
WHY ??
[ July 07, 2004: Message edited by: Giju George ]

SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCEA
mini mehta
Ranch Hand

Joined: Oct 22, 2000
Posts: 120
Originally posted by Giju George:
A stateless session bean can call the EJBContext.getUserTx() in the ejbCreate() and ejbRemove(). But, then why can't it NOT invoke the user tx methods there ???


It can't call the Container managed transaction because the during these methods, container doesn't have a valid transaction. But u can create your own transactions using EJBContext.getUserTransaction();

Also


In a stateful session bean's ejbActivate and ejbPassivate, we can call getCallerPrincipal() and isCallerInRole().. where as we cannot do the same in entity bean's ejbActivate and ejbPassivate...........
WHY ??


After ejbPassivate the entity beans goes into a pool state, that means it is not attached to a client and similarly when ejbActivate is called bean is in the pooled state and hence no client.
[ July 07, 2004: Message edited by: Giju George ]
Giju George
Ranch Hand

Joined: Jun 08, 2004
Posts: 333
well, then why can we call the methods in stateful session bean's ejbActivate and ejbPassivate ?? There also it's not attached to a client !!!
[ July 07, 2004: Message edited by: Giju George ]
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi Giju,

Could you explain me, How will you get the EJBContext in session beans? setSessionContext method of the session bean is called with the parameter, SessionContext not with the EJBContext.

Where as for your second question, in statefull bean's ejbActivate and ejbPassivate methods, you are having the reference to the client, hence you can call getCallerPrincipal() methods.

I hope i am correct...


Narasimha
sean mullins
Greenhorn

Joined: Mar 22, 2004
Posts: 3
The thing to remember about ejbActivate and ejbPassivate is that they have completly different meanings for Session beans versus Entity beans.
HFEJB has a good synopsis on page 308.
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
Narasimha, SessionContext extends EJBContext ...
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
It can't call the Container managed transaction because the during these methods, container doesn't have a valid transaction. But u can create your own transactions using EJBContext.getUserTransaction();


- You can only call EJBContext.getUserTransaction(); if you are using BMT and the Container has nothing to do here.

- To create your own transaction you have to call methods on UserTransaction and the point is you can't do that! Or is it the case that you can call some methods but not others??? In this point I have the same doubt as Giju: if you can't call methods on it then why the heck can you get a ref to UserTransaction in the first place???

As for activate/passivate Giju, for stateful session beans they are used by the container when a client takes too long between calls. The bean is sent to sleep whith passivate but the client still exists. When the bean wakes up (because the client finally made another call) it can also access the client's info. The client is there all along!

For entity beans activate/passivate do something different. When a bean goes out of the pool to "play the role" of a particular entity (Jhon Malcom, #28) ejbActivate is called. At this stage it doesn't know who the client is, it's just focused on becoming Jhon Malcom. When the bean finishes it's performance and goes back to the pool, ejbPassivate is called. The bean doesn't know who the client was anymore and it doesn't care. It just needs to stop playing Jhon Malcom, go back to the pool and be just a bean again. See pages 348-355 of HFEJB.
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
A stateless session bean can call the EJBContext.getUserTx() in the ejbCreate() and ejbRemove(). But, then why can't it NOT invoke the user tx methods there ???


As far as I know (from HFEJB), stateless session bean ejbCreate()/ejbRemove() can only do these:
1. get EJBHome object
2. get EJBObject
3. get reference to the JNDI context

It cannot call EJBContext(or more precisely SessionContext.getUserTransaction()) to get the UserTransaction.

Am I right?
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
See page 90 of the spec. You can call getUserTransaction but you can't call methods in the UserTransaction you get.

Can anyone explain why you can get the transaction if you can't use it???
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Yes, you are right, slsb ejbCreate can call getUserTransaction() but not to call any method on it.

A bit (actually very) disappointing with HFEJB. As this is not the first time it gives me some technical info not consistent with the spec. This is not the only place it happens, e.g. P406 mentions the dot navigation cannot terminate on CMR field, but this is incorrect. Spec says so long as the CMR field returns a single value it is allowed.

I just wonder how many wrong info given by HFEJB
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
They miss some details here and there but look at how large the content for this exam is ... no single book can tell everything ... I think it's up to us to dig into the details like we're trying to do here ... too bad none seems to be answering ...
 
 
subject: operation allowed in session and entity beans
 
Similar Threads
why do we need ejbactivate and ejbpassivate
SLSB Pool & Entity Bean Pool
question on activation/passivation
Calling getCallerPrincipal method..
client security context