This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
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

Head First EJB Chapter 6, question 15

Colin Richardson

Joined: Jan 23, 2004
Posts: 13
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

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 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

Joined: Oct 10, 2002
Posts: 1589
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,
Colin Richardson

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

Joined: Feb 11, 2003
Posts: 117

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.


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="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: Head First EJB Chapter 6, question 15
It's not a secret anymore!