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 Head First EJB Chapter 6, question 15 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 "Head First EJB Chapter 6, question 15" Watch "Head First EJB Chapter 6, question 15" New topic
Author

Head First EJB Chapter 6, question 15

Colin Richardson
Greenhorn

Joined: Jan 23, 2004
Posts: 13
Hi,
Head First EJB Chapter 6, question 15 (pages 366/371) asks which methods are always invoked in direct response to a client operation, and the answer says that ejbCreate() and ejbRemove() are, and ejbLoad(), ejbACtivate(), ejbPassivate() and setEntityContext() are not.
At the moment I don't understand when ejbActivate() is called other than as a direct result of a client request. It's called when the bean moves out of the pool to the method ready state - doesn't this mean that it is as the result of a client request? Does the bean move to the method ready state for other reasons?
The note with the answer on page 371 says that Load, Passivate and setEntityContext can be called by the Container when it wants to, but it doesn't mention anything about ejbActivate().
[ February 05, 2004: Message edited by: Colin Richardson ]
Colin Richardson
Greenhorn

Joined: Jan 23, 2004
Posts: 13
Can anybody offer any help on this? Page 172 and 175 of the spec says that ejbActivate() is called when the bean instance is taken from the pool and assigned a specific identity. Does this ever happen other than as a result of a client operation?
There's probably something obvious about ejbActivate() that I'm not seeing, but I wonder if the answer in HFEJB is correct? I've checked the errata on oreilly.com and there's nothing mentioned, and I also did a search on this forum to see if I could find any previous questions on the same subject, but again didn't find anything. This suggests that it must just be that I've missed something obvious...
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy -- this is a good question Colin, and it's confusing because we don't really explain what "as a direct result" means.
When we say "as a direct result", that means the method is invoked because the client called a corresponding method in an interface. Even ejbActivate() can be called whenever the Container wants to... although you're right, most of the time it is called because the client invoked a business method that starts a new transaction. But the Container can do whatever it wants with activation and passivation behind the scenes. Unlike stateful session beans, an entity bean can be passivated even while it's in a transaction. So, even in the course of one transaction triggered by one initial call from the client, there could be activation/passivation going on (the bean always gets the ejbStore() before passivation, though... that much IS guaranteed.)
So, we want you to separate out the ones that are *always* triggered as a DIRECT result of a client call from the ones that *might* be triggered as the direct result of a client call. It's the word "direct" that matters, but I agree that this is confusing because we didn't define what we really mean by "direct", and ejbActivate() is a fuzzy area, but just remember that what the Container does behind the scenes is anything the Container wants to do... as long as the behavior is synchronized with the database properly.
When you see "direct" think, ALWAYS invoked as opposed to MIGHT be invoked. If client calls a business method, or any other method on a bean, there is never a guarantee that the other intermediate methods are called (including ejbActivate and ejbLoad).
But I think your point was that even if it isn't called EVERY time, the times when it IS called are always a result of a client invocation, right? That's how it looks, yes. But in the spec (page 169) it gives the Container flexibility to do whatever it wants there. I should have pointed this out more specifically... your conclusion is completely reasonable!
So when you see "as a direct result" think only of the things which are ALWAYS invoked (unless the Container refuses for security reasons or errors) and you should be safe. If something might or might not be invoked as part of the underlying process of getting the bean ready, then don't include it as a direct result.
Hope that helps,
Kathy
Colin Richardson
Greenhorn

Joined: Jan 23, 2004
Posts: 13
Kathy,
Once again, thanks for another great answer. It's much appreciated.
Colin
Vince Hon
Ranch Hand

Joined: Feb 11, 2003
Posts: 117
Colin,

I think on HFB book P.356 ("Commit Option A and B"), that mentioned a situation that ejbActivate() was not called when client called the bean operations.

Kathy,

Unlike stateful session beans, an entity bean can be passivated even while it's in a transaction


Is it one of the possible case ?

Bean A may be passivated when it's in a transaction. Am I correct ?


Vince Hon<br /> <br />SCJP 1.4 | SCWCD | SCBCD <br /><a href="http://vincehon.homeip.net:8000/VJW" target="_blank" rel="nofollow">http://vincehon.homeip.net:8000/VJW</a>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Head First EJB Chapter 6, question 15
 
Similar Threads
Where an entity bean should acquire additional resources ?
Doubts in Chapter-6 HF-EJB
getEJBObject from ejbPassivate,ejbActivate
dbt on Missed ejbRemove()
question 15 HF pg 366