While I agree that simply returning the primary key is more efficient then checking
for the entity in the database, there are drawbacks to this strategy.

Here is the problem: If the entity doesn't exist in the database then the activated
instance will throw an exception in its ejbLoad method which will result in a
transaction rollback.

This rollback is avoided with the ejbFind method that checks for the entity, because
it  throws an application exception (ObjectNotFoundException), which doesn't cause a
transaction rollback.  Generally we don't expect the ejbFind method to cause a
transaction rollback if there is no entity is found.  In addition, the contract of
the find operation is broken. Instead of throwing an ObjectNotFoundException, as
expected, the find method will throw a RemoteException.

As long as the bean developer and application developer are aware of these
differences the change in behavior (which does not conform with the specification)
is perhaps tolerable.

Richard
--
Richard Monson-Haefel
Author of Enterprise JavaBeans, 2nd Edition
Published by O'Reilly & Associates
http://www.EjbNow.com

David Pinnington - Sun UK - Consultant wrote:

> I personally do exactly this.
>
> If you image the finder method actually doing a database lookup then you will be
> doing the following:
>
> ejbFindByPrimaryKey() - does database lookup (SELECT)
> if Entity Bean not loaded into server then
>         create new EJB inmemory and populate using ejbLoad - (SELECT)
>
> So in this case you have done 2 database lookups which i think is much more
> expensive. The EJB clinet will still get the FinderException whichever way you
> do it so I always go with the finder simply returning the PK without doing a
> lookup.
>
> In fact I would go one step further - if ever I have a finder method (primary
> key or not) where I can get / calculate the primary key withough doing a
> database lookup I will (e.g. composite key etc).
>
> davep
>
> >
> > Hi,
> >
> > I just saw a bea slide which recommends to implement ejbFindByPrimaryKey by
> > just returning the parameter:
> >
> > String ejbFindByPrimaryKey(String pk)
> > {
> >      return pk;
> > }
> >
> > This code does not perfom a database lookup.
> >
> > The above coding has consequences:
> > 1. if a bean with the specified pk does not exist, ejbFindByPrimaryKey will
> > not throw a FinderException. Instead, ejbLoad will fail (in which case a
> > FinderException can be thrown).
> > 2. No database lock is set to the tuple with the pk.
> >
> > Has someone out there experiences with the optimized code?
> >
> > Thanks,
> > Marc Volz
> > TLC, Germany
> >
> > ===========================================================================
> > 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".
> >
>
> ----------------------------------
> Dave Pinnington - Sun Microsystems
> [EMAIL PROTECTED]
> tel: +44 (0)1223 418868
> fax: +44 (0)1223 420257
>
> ===========================================================================
> 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".

===========================================================================
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".

Reply via email to