12

Environment: Hibernate 4.2.6

Scenario: hibernate logs “ERROR” for a specific "unique constraint PK_XXX violated" sql exception. However due to our specific multiple instance deployment scenario, we need to tweak this to “INFO” level for this specific "unique constraint PK_XXX violated" sql exception. (So it does not confuse IT folks).

In other words, technically it is "ERROR", but it is "INFO" from business perspective.

Currently:

2016.01.19 13.20.14,299 [ ERROR ][ ][ ][ ] org.hibernate.engine.jdbc.spi.SqlExceptionHelper - logExceptions() ORA-00001: unique constraint (PK_LOCKINFO) violated

Desired:

2016.01.19 13.20.14,299 [ INFO ][ ][ ][ ] org.hibernate.engine.jdbc.spi.SqlExceptionHelper - logExceptions() ORA-00001: unique constraint (PK_LOCKINFO) violated

What is the easiest way to go forward? Options I could think of:

  1. Modify hibernate internal class "org.hibernate.engine.jdbc.spi.SqlExceptionHelper" , then recompile the “hibernate” jar

The specific customization is: in case of "unique constraint PK_LOCKINFO violated" sql exception, log it as "INFO" level instead of "ERROR" level. But any other sql exceptions should still be logged as "ERROR" level. This option poses a challenge as we go forward. We will have to do it every time hibernate upgrades.

  1. have a class to extend & customize hibernate class "org.hibernate.engine.jdbc.spi.SqlExceptionHelper" in our own codebase This way we do not have to compile a “hibernate” jar. Is this possible?

  2. Add a after-the-fact-listener in our code which scans through "log" constantly, then do something about it

Not sure if I like option #3 or not. Seems too awkward.

Any other options?

riceball
  • 403
  • 2
  • 15
  • 1
    So, such exceptions would be logged as INFO even if they don't originate from '_your specific multiple instance deployment scenario_'? Even when they happen because of a bug causing a _real_ PK violation? If you ask me, I would refactor your deployment scenarios and let PK violations be logged as errors. Also, you didn't mention which logging provider you use. Anyway, I would try to utilize the provider capabilities, for example [log4j custom appender](http://stackoverflow.com/questions/7756308/log4j-modify-logged-message-using-custom-appender) or similar. – Dragan Bozanovic Jan 21 '16 at 17:51
  • Though the post is quite old posting it, what if I want to catch this exception to handle it. Not able to get a hold of this exception. Any suggestions ? – Raghuveer Mar 31 '17 at 10:58
  • @Raghuveer Sorry just saw your question. Your own code should be able to catch this kind of exception – riceball Sep 06 '17 at 21:13

1 Answers1

0

We ended up going option #2: have a class to extend & customize hibernate class "org.hibernate.engine.jdbc.spi.SqlExceptionHelper"

riceball
  • 403
  • 2
  • 15
  • 6
    Can you please elaborate how to connect the extended class with the spring? I made a class extending SQLExceptionHandler and override the methods. what to do next? – Michelle Sep 02 '20 at 14:09
  • 3
    It would be nice how you integrated the SqlExceptionHelper extension into the spring framework. SqlExceptionHelper gets instantiated by JdbcEnvironmentImpl's constructor and it is final. How can you inject your extension? – Manuel Apr 12 '21 at 13:02