users@glassfish.java.net

Re: Unexpected Behavior: NoResultException rolls back transaction

From: <glassfish_at_javadesktop.org>
Date: Tue, 29 Jan 2008 01:58:49 PST

You are welcome :)

It is indeed a little bit confusing, since you have to differentiate whether this runs within a container or not. I think a note should be made in the Javadocs for this!

I had interesting bugs because of this situation as well, I mistakenly thought that one my methods would catch this NoResultException, and actually did not even bother to catch that specific type of exception, but instead I did something like this:
methodA:
try
{
    resultsLits = myFacade.getQueryResults()
}
catch(Exception e)
{
..//errors in select queries
}
//more calls to other facades updating db.


As you can guess, I was catching the TransactionRollbackException instead... When the code continued, it was trying to do some more updates from within the same method (methodA), and I had exceptions of type: TransactionRequiredException, which puzzled me even more.

To me it seemed that a "simple" failure of not returning results from a query was actually causing the Container to somehow void the whole transaction!

It was a revelation when i discovered what exception is actually thrown from the container ;)

My solution for this was to deal with the NoResultException within myFacade and simply return null when nothing is found.

Thanks all as well :)
[Message sent by forum member 'mmichalak' (mmichalak)]

http://forums.java.net/jive/thread.jspa?messageID=256284