How to shut off an error or warning on a per query basis using Hibernate

I am performing a query and I’m expecting lock to fail sometimes. I am catching the exception but i do not want the warning and errors bellow going to log. I want to control this per query or session I don’t want to shut the logging off completely.

04:14:32,107 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (EJB default - 1) SQL Error: 0, SQLState: 55P03
04:14:32,109 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (EJB default - 1) ERROR: could not obtain lock on row in relation ...
04:14:32,110 ERROR [stderr] (EJB default - 1) Exception org.hibernate.PessimisticLockException: could not extract ResultSet
04:14:32,110 ERROR [stderr] (EJB default - 1) Exception message could not extract ResultSet

Hibernate, like many other frameworks, does not provide this feature.

The logger can be either activated or disabled. You can disabled it for a single method call. The only way to achieve that is if you parse the log using a tool like Logstash and you filter out the ERROR message that contain a given MDC key. Hibernate uses JBoss Logging which also supports MDC. For more details about MDC, check out this article. Basically, you will have to inject an MDC key in all log entries for a given data access method so that you can ignore all ERROR and WARN messages containing that MDC key.