persistence@glassfish.java.net

RE: How to detect optimistic lock exception?

From: Gordon Yorke <gordon.yorke_at_oracle.com>
Date: Wed, 7 Nov 2007 15:33:45 -0500

Seems like there may be a container issue here where the container is not including the exception from the PersistenceProvider in the RollbackException.
--Gordon

-----Original Message-----
From: Ellen Kraffmiller [mailto:ekraffmiller_at_hmdc.harvard.edu]
Sent: Wednesday, November 07, 2007 2:22 PM
To: persistence_at_glassfish.dev.java.net
Subject: Re: How to detect optimistic lock exception?


Gordon & Marina,
After doing some more testing, I found some curious behavior -

If I don't call flush() in my session bean, then an EJBException is
thrown, with the following results:
ejbException.getCausedByException() returns a RollBackException
ejbException.getCausedByException().getCause() returns null
(So, I don't see a way to use "getCause()" to get to the root
OptimisticLockException )

If I add flush() to my session bean method, I get the following results
on the client:
ejbException.getCausedByException() returns null
ejbException.getCause() returns OptimisticLockException

So, it seems that I have to explicitly call flush() with the session
bean to detect an OptimisticLockException on the client side.
Is this correct behavior? It seems awkward to have to add a flush()
just to get the correct exception on the client.

Thanks,
Ellen


Gordon Yorke wrote:

> If you need to process the Rollback Exception the exceptions are all
> chained so if you continue to check "cause" on all the exception
> starting with the EJB Exception you should find the OptimisticLock
> Exception at the root.
> -Gordon
>
> Marina Vatkina wrote:
>
>> 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
>>
>