views:

23

answers:

2

I have just implemented exception handling for a unique-constraint of a JPA entity. It is working as I want it to, but when triggered dumps the handled exceptions to the container logfile.

  • A JPA entity is managed by a SLSB (Service Façade). The Service Façade is called from another SLSB, which provides remoting capabilities based on JAX-RS.
  • In the Service Façade, the EntityManager operations are wrapped in a try-catch-block, detecting the cause of the unique-constraint-violation. It then throws a custom checked ApplicationException.
  • The REST-Bean catches the ApplicationException and throws a custom unchecked BadRequestException.
  • An ExceptionMapper outputs the BadRequestException to the remote client.

This is all working well. The part that I don't understand is: the (handled) exceptions get logged in the container's logfile (complete with a long stacktrace):

[#|2010-09-29T18:49:39.185+0200|WARNING|glassfish3.0.1|org.eclipse.persistence.session.file:/Users/hank/NetBeansProjects/CoreServer/build/classes/_coreServerPersistenceUnit|_ThreadID=30;_ThreadName=Thread-1;|
Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry....
....
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry....

and from throwing the BadRequestException:

[#|2010-09-29T18:49:39.336+0200|WARNING|glassfish3.0.1|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=30;_ThreadName=Thread-1;|A system exception occurred during an invocation on EJB ShopperResource method public javax.ws.rs.core.Response mvs.gateway.ShopperResource.create(javax.xml.bind.JAXBElement)
javax.ejb.EJBException
at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5119)
....
Caused by: mvs.api.exception.BadRequestException: mvs.api.exception.MvsCause: Field 'MSISDN' must be unique!

Is this how it should be? I thought since I handle the exceptions, they wouldn't be dumped to the log?

A: 

It's the database layer that does the logging of the exceptions. The time you catch them they are already written to the log.

tangens
+2  A: 

The exceptions are logged because you have exception logging enabled.

Exceptions get logged by default when your log level is WARNING or greater. If you set your log level to SEVERE or OFF then they will not be logged.

i.e. "eclipselink.logging.level"="SEVERE"

You can also set the "eclipselink.logging.exceptions"="false" property to disable just exception logging.

See, http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging

James