Home > Enterprise >  How must I handle database and JPA exceptions?
How must I handle database and JPA exceptions?

Time:03-18

Given the method below how should I deal with possible exceptions that may arise? I want to provide feedback at database level and JPA level. When a constraint violation is thrown, I want to know on what field. How would you handle exceptions of this method?

public static <T> void persist(T entity) {
    try {
        em.getTransaction().begin();
        em.persist(entity);
        em.getTransaction().commit();
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
    }
}

CodePudding user response:

Three distinct problems

  1. Usually there are Exceptions that are caused by the user or bad frontend programming, like supplying invalid IDs (entity not found exceptions etc)
  2. Then there's exceptions due to bad code.
  3. And finally, exceptions stemming from the infrastructure.

Reaction

For error #1 you should echo back those exceptions to the frontend, by replying with a 4xx error / status code, or any other visual feedback to the user.

The other two are system errors that the programmer or hardware guys have to take care of. I usually catch those exceptions, then pack it into an email, and send it to the support groups and programmers, depending on the exception. I also echo that error back to the user, if possible with some clarifying message.

Examples

  1. Now in your case error #1 is harder to achieve.
  2. An example for Error #2 would be that null is passed as an argument, or that something inside entity does not like serialization. Those are cases the programmer needs to be informed of.
  3. For error #3 there's the "disk full" and "database disconnected" and other "hardware-not-available" problems. Those need to go to the maintenance group.

Strategy

  1. Put default calls like this in a library of your own
  2. Create custom exceptions to have at least one (base) class for the three cases. In the servlet context, you can also fit those Exceptions with an HTTP status code that should be returned to the frontend.
  3. Extend all the calls in the library to check for exceptions and throw one of your custom exceptions.
  4. On the highest level of implementation possible handle the exceptions. For servlets I have a ErrorHandlingServlet extends HttpServlet and each servlet of mine uses that, like in your case it would be SaveUserServlet extends ErrorHandlingServlet or something along that line. The ErrorHandlingServlet then sends an Email if necessary, and returns the HTTP status code included in the custom exception.

Personal Examples

I go as deep as having the normal ParameterInvalidException, ParameterMissingException, EntityNotFoundException. Plus I also have some very specific exceptions like UsernameNotFoundException (really depends on the scope of the app whether you wanna use that), or InsufficientRightException (HTTP 401), or EntityStillAttachedException.

Facit

There's a multitude of those you can cook up, and it's up to you to decide when to use and how to handle those.

  • Related