posted 20 years ago
Howdy -- this is a good question, and one that gets a lot of people.
Jats, you really should think of an entity bean has having two separate jobs--ONE is to *be* an actual entity ("Now I am John Smith, PK # 72) and another job is to be a "finder/home business method service on behalf of ALL entities of my type". When a bean is performing that second job, it is acting almost like a stateless session bean, in that one entity of that type is the same as any other entity of that same type. So, a Customer entity bean in the pool, running a Finder method, is the same as any other entity bean.
The confusion is about what happens when the bean is asked to run a single-entity Finder, like findByPrimaryKey("24")
In this case, the bean does not ever BECOME entity #24, Bob Izarra. Instead, the bean just LOOKS to be certain that #24 is actually IN the database. That's all. The bean is never populated with Bob (#24) Izarra's data. The bean never BECOMES Bob Izarra. It simply runs the Finder, checks to see that yes, Bob is in the database, and returns the PK as confirmation. So after a Finder, the bean's instance variables are NOT set and the bean is NOT in a business-method-ready state. It's simply still sitting in the pool waiting to run a Finder/Home Business method OR to come out of the pool through ejbActivate() or ejbCreate().
So, the question is WHY doesn't the bean come out of the pool and become Bob Izarra? After all, that means TWO hits to the database... one to do a select to see if Bob, #24, is in the database, and then ANOTHER once the client wants to USE that bean to invoke a business method.
Yes, it seems a little less efficient if the client really DOES want to USE the bean once they've found it (and in fact, some servers *do* let you tune for this, but it is not part of the spec).
But... it is MORE efficient to do it this way overall, because once the client has obtained a *reference* to the bean (the purpose of a finder), then the next time the client calls a business method, that bean's data may have changed in the DB, and another SELECT will have to run anyway (which means another ejbLoad()). So, by keeping the bean in the pool, it saves the Container from having to load all of the bean's data and populate the bean's fields UNTIL the client actually *gets serious* and invokes a real business method. At that time, the Container says, "OH, I guess you need some real data loaded in the persistent fields, so it does a SELECT on whatever other data it needs to now use to populate the bean's fields.
There are other issues that make it a little more complex, but as far as the specification is concerned (and the exam) THIS is how it works. Always. Bean stays in the pool to run Finders or home business methods, so once a client *finds* a bean and thus gets a reference to the bean's EJBObject, the bean won't necessarily be loaded and populated until the client USES the reference to invoke a business method.
Hope that helps...
- Kathy