[hibernate-issues] [Hibernate-JIRA] Created: (HHH-5837) Hibernate should no log exceptions on its own - just throw them and let the service layer decide

Grzegorz Borkowski (JIRA) noreply at atlassian.com
Thu Jan 13 05:04:05 EST 2011


Hibernate should no log exceptions on its own - just throw them and let the service layer decide
------------------------------------------------------------------------------------------------

                 Key: HHH-5837
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5837
             Project: Hibernate Core
          Issue Type: Improvement
          Components: core
    Affects Versions: 3.5.3
            Reporter: Grzegorz Borkowski


Example:
In an invoicing application there is a constraint on an invoice table, ensuring that invoice numbers must be unique. This column is not a primary key, for some reason. So, there is a unique constraint at database level on this column. When the user creates duplicate record, I expect that Hibernate just throw the appropriate exception. and the service layer will process correctly this exception and  tell the user "pls choose a different number".
But currently Hibernate also logs several (!) error lines in log file, when it happens, like this:
o.h.e.d.AbstractFlushingEventListener - Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
o.h.util.JDBCExceptionReporter - Duplicate entry '...' for key '...'
o.h.util.JDBCExceptionReporter - SQL Error: 1062, SQLState: 23000

The problem is that in our setup any error logged, causes new e-mail being sent to system admin - because the error means something went wrong and admin should take care of this. But in this case its nothing wrong with the system - the application is able to handle such errors, so no error should be logged. Generally it's considered to be the good practice that low-level layers and libraries (like ORM) shouldn't take business-level dicisions. Deciding on whether to log an error message to log file or not is a business-level decision, so let the appropriate leyer decide - it's not Hibernate responsibility. Just throw the appropriate exception.

Exactly the same situation happens when deadlock is detected. We have a kind of intercepor in front of service layer, that automatically restarts transaction when deadlock is detected. So again it's this interceptor responsibilty to either restart the transaction automatically, or log the erro (e.g. after some number of repeates). But the same problem here: Hibernate takes the responsibilty on itself and print several (again several - very bas practice!) log statements:
o.h.e.d.AbstractFlushingEventListener - Could not synchronize database state with session org.hibernate.exception.LockAcquisitionException: Could not execute JDBC batch update
o.h.util.JDBCExceptionReporter - Deadlock found when trying to get lock; try restarting transaction 
o.h.util.JDBCExceptionReporter - SQL Error: 1213, SQLState: 40001 

(BTW it would be much better if some special exception was thrown in such a case, so that we can take decision based on exception class - now we have to parse the message string, searching for 'try restarting transaction' text - very fragile).

Obviously, we can turn the logging off for Hibernate loggers, but it's not perfect solution too (e.g. then we will miss any warnings generated potentially by those loggers).

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list