openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Judes Tumuhairwe" <>
Subject Re: how do you manage db exceptions?
Date Sun, 30 Nov 2008 02:31:02 GMT
It depends.
- If you're hand coding everything yourself, I would suggest some kind of
observer-listener approach where you listen for specific the exceptions &
take the necessary action. If you're using a framework like JBoss Seam, for
example, this can be done declaratively (application wide). I'm sure
Spring[1] and other frameworks have some kind of implementation of that
design pattern.

- The other (less elegant) option is to  try/catch all 8 exceptions in the
JPA (EntityExists, EntityNotFound, OptimisticLock, NoResult,
TransactionRequired, etc. etc.) in the webMethod of your web-service (or the
ExceptionInterceptor in as it may be) and tell the client what happened.
Obviously this assumes that the persistence-manager didn't change/wrap the

> the only way to make this that comes to my mind is to catch the exception
> and look for some keyword exception in the stack trace and then find out
> what is the cause
The bad news is OpenJPA has its own exceptions [e.g. ObjectNotFoundException
instead of javax.persistence.EntityNotFoundException]; the good news is you
can just catch their parent StoreException and call getType()

[1] <>


On Sat, Nov 29, 2008 at 10:17 AM, is_maximum <> wrote:

> Actually this question is related to EJB 3 but I since I am using OpenJPA I
> thought it was better to put my question here.
> when you are using merge() or persist() methods of EntityManager, these
> methods may throw SQLException wrapped in PersistenceException and finally
> at the end of my business method I will catch an EJBRollbackException or
> EJBRuntimeException, I don't want to catch it to recover from the situation
> but want to return back an appropriate message. for example, notify the
> user
> that this record has at least one child record so you can't remove it or
> another record with this information is already exists and etc.
> because we are using webservices and our clients application are developed
> in .NET platform we have a ExceptionInterceptor to catch all the exception
> and send back an appropriate error code to notify the user.
> the only way to make this that comes to my mind is to catch the exception
> and look for some keyword exception in the stack trace and then find out
> what is the cause
> I am wondered why they didn't take this into account while they have
> provided EntityExistException but not the other states.
> Wes Wannemacher wrote:
> >
> > I'm not sure if this is a question specific to openjpa, but it doesn't
> > seem like it, so I will answer as though it is not. In general, whenever
> > working at a level that would encounter a Sql Exception, I catch them
> > and wrap them in an application specific Exception that is unchecked. I
> > don't think I've ever had a SQLException that application code can
> > recover. Or, at least, never have I come across a situation where the
> > effort to recover from the exception is worthwhile.
> >
> > Lately, I have been using Spring quite a bit... Within Spring's database
> > APIs, SQLExceptions are caught and wrapped in much more useful
> > exceptions that are unchecked.
> >
> > -Wes
> >
> --
> View this message in context:
> Sent from the OpenJPA Users mailing list archive at

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message