In Java PetStore and in Sun's J2EE RI tests I see member variables being set
in the bean instance during a findByPrimaryKey call, and I wonder how this
is supposed to work. The EJB 1.1 spec (page 103) seems fairly clear the
instance method is used to execute the finder, and then the instance is
returned to the pool; so how can it be useful to attach identity-specific
data such as an accountId to the bean instance? Is there some setting of
transactional attribute or something that keeps the bean instance active and
attached to the same EJBObject? Can somone point me to the part of the spec
that clarifies how this works?
For example, we see code like this:
public class AccountEJB implements EntityBean
{
public String accountId; // primaryKey
public int balance;
...
public String
ejbFindByPrimaryKey(String primaryKey) throws RemoteException
{
System.err.println(
"In ejbFindByPrimaryKey, primaryKey = "+primaryKey);
accountId = primaryKey;
// Try to load the row for this accountId
loadRow();
return primaryKey;
}
...
private void
loadRow() throws Exception
{
// jdbc code to get a ResultSet
...
balance = result.getInt(1);
}
// other methods use accountId and
// balance as instance vars
public void
credit(int amount) throws RemoteException
{
System.out.println("In credit for "+accountId);
balance += amount;
}
// ejbLoad and ejbStore also call loadRow
public void ejbLoad() throws RemoteException
{
System.err.println(
"In ejbLoad, accountId="+accountId);
loadRow();
}
}
The instance data is *not* read out by ejbPassivate/ejbActivate in the code
I see in PetStore, so I think it must be lost when/if we passivate the bean
and re-activate in another instance body ...
As a faint seconndary question: even if is somehow legal, is it as
inefficient as I think it is? The DB call is done in the findBy<> and then
again on the required ejbLoad() when the bean transitions into the ready
state ... so you end up with two database accesses to get the bean available
the first time!
I must be missing something... thanks for passing a little clarity my way!
Wayne
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".