persistence@glassfish.java.net

Re: How to detect optimistic lock exception?

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Tue, 06 Nov 2007 14:20:24 -0800

Hi Ellen,

You need to rethrow the OptimisticLockException caught after flush in the bean
code as an application exception. The transaction will be rolled back anyway,
but the client will get your own message.

Regards,
-marina

Ellen Kraffmiller wrote:
> Hi all,
> I have a standard web application, using a stateless session bean to
> save changes made in a web form. If an optimistic lock exception occurs
> (for example if the user tries to update the same data in two different
> tabs in a browser), I'd like to display a specific user-friendly
> message, rather than a generic rollback exception. The problem is I
> can't find a way to detect the optimistic lock exception from within the
> EJBException object that is thrown. The getCausedByException() method
> returns a RollBackException object, but this object has no information
> detailing what caused the rollback. If I look in the server.log, I can
> see that an OptimisticLockException was thrown within Toplink. Is there
> any way I can get this information at the application layer? (Btw, I
> tried calling flush() to try to trigger the exception within the session
> bean method, but this didn't help - the rollback exception isn't thrown
> until the container commits the transaction, after the session bean
> method is completed.)
> Thanks,
> Ellen