How to disable SHOW WARNINGS from Hibernate?

Using Hibernate (4.3.8) with MySQL, I noticed a lot of SHOW WARNINGS occupying a significant bandwidth in the activity log:

enter image description here

I searched around, and this is a fairly common problem ( for example ), which, apparently, can be solved by increasing the log level to ERROR (and this solution is confirmed implemented at least from 4.3.6).

The problem is that I really don't know how to do this. My knowledge of Hibernate is about the minimum minimum required to work with it. Previously linked mail resolved it by changing the log settings in logback.xml , but I do not use Logback. I use all the default settings:

  • It appears to be using JBoss Logging in its core.
  • I have no other registration dependencies in my class path (e.g. slf4j.jar), so I definitely do not use them. Log messages are simply written to System.err .

So I'm not sure how to do this. Here is my configuration file:

 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/xxxxx</property> <property name="connection.username">xxxxx</property> <property name="connection.password">xxxxx</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.isolation">2</property> <property name="connection.pool_size">10</property> <property name="current_session_context_class">thread</property> <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- <property name="show_sql">true</property> --> <!-- <property name="hbm2ddl.auto">create</property> --> <mapping resource="hibernate.hbm.xml"/> </session-factory> </hibernate-configuration> 

Here are the dependencies in the build path; there Jettison and Joda, the MySQL driver, and then everything from the Hibernate required dependency directory and nothing more:

enter image description here

How to increase the level of the log or otherwise disable SHOW WARNINGS in this case (by default)? This refers to the "categories of logs of interest", but I'm not sure how they relate to the configuration file. This page does not have any log-related properties documented outside the Logging section, which mentions SLF4J, but apparently I am not using SLF4J (as I could be, since it is not in my class).

+3
source share
2 answers

Good. I got this and learned a lot in this process.


TL DR:

Under the following assumptions ...

  • Recording parameters are not set explicitly.
  • The only JAR log related paths to the classes are the jboss logs (i.e. no slf4j, log4j, etc.).

... Hibernate will automatically select JDK logging through JBoss logging , and java.util.logging can be used to control the log level. Therefore, setting the log level of all JDK logs to SEVERE , at the root logger level (it doesn’t matter if you do this before or after Hibernate initialization, see below) successfully stops the SHOW WARNING commands:

 LogManager.getLogManager().getLogger("").setLevel(Level.SEVERE); 

However, this is a shotgun approach that sets the log level for all registered Logger for which their level is set to null (inherited from the parent).


JDK Protocol Notes

I came to the above solution:

  • Reading the doc log several times until it dies, and without comparing this information with what was in my class, evidence showed the JDK log was used.
  • Looked through a list of log names with LogManager#getLoggerNames() . It was a lot from Hibernate, this confirmed point 1.
  • I tried to find the registrar responsible for SHOW WARNINGS , but could not. I know that this is not "org.hibernate.SQL" (changing this level of the log has no effect), and it is not "org.hibernate" (this log does not actually exist).
  • I printed out the current log levels for all of them and noticed that they are all null (inherit from parent).
  • I printed out the hierarchy and saw that the root logger (name ") was the only one that had the log level set, so I set this log level. Note that since Hibernate logs are usually set to" inherit ", it doesn't matter if you run is this before or after setting up Hibernate: if you do this before you get the registration, if you do it after you still see that it is displayed during initialization and probably a couple more SHOW WARNING will be issued, but this doesn't really matter.

So, as I came to the above, and this leaves the following negligence in my decision:

  • The shotgun approach is not entirely accurate which registrar is responsible, so they are all disabled.
  • Confused by the lack of "org.hibernate".
  • This only works when using the JDK journal, which relies on the above assumptions. Including another supported logging structure in your classpath may cause Hibernate to choose a different structure, which makes the solution ineffective.
  • I just tried SEVERE and checked that it works. Different levels of logs may also work, but I have not tested.

Other notes

I tried to explicitly find out which of the sleeping Hibernate was automatically selected, but could not determine how to do this (does anyone know?). My only wild hunch was something in ServiceRegistry , but there didn't seem to be anything related to the Service journal, or at least I could not find it.

In general, it is much more predictable to explicitly tune the logger and then tune it according to its documentation, because, for example, the above solution breaks simply by adding another JAR to the class path. However, for quick / one-time / non-essential projects where you simply collapse configuration files, etc., This is apparently the easiest way to cut out SHOW WARNING commands.

0
source

The hibernate structure allows MySQL SHOW WARNING by default for each running query, this doubles the number of queries to MySQL, and the application can implement performance problems. This additional SHOW WARNING logging using sleep mode can be set using

 org.hibernate.engine.jdbc.spi.SqlExceptionHelper#handleAndClearWarnings() 

Decision

Make hibernation by choosing the right registrar. This can be done by adding: -Dorg.jboss.logging.provider=slf4j or -Dorg.jboss.logging.provider=log4j as the JVM runtime parameter.

For slf4j logger you need to configure the logback.xml file. Add this:

 <logger name="org.hibernate.type" level="ERROR" /> 

For log4j logger you need to add the following line to log4j.properties :

 log4j.logger.org.hibernate.type=ERROR 
0
source

All Articles