Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable SHOW WARNINGS from Hibernate?

Using Hibernate (4.3.8) with MySQL, I noticed a bunch of SHOW WARNINGS statements taking considerable bandwidth in the activity log:

enter image description here

I searched around and it's a pretty common issue (for example) that can apparently be resolved by increasing the log level to ERROR (and that solution is confirmed implemented since at least 4.3.6).

The problem is, I don't actually know how to do this. My knowledge of Hibernate is about the bare minimum necessary to work with it. The previously linked post solved it by editing Logback settings in logback.xml but I'm not using Logback. I'm using all the default settings:

  • Apparently it uses JBoss Logging at its core.
  • I don't have any of the other logging dependencies in my classpath (e.g. slf4j.jar) so I'm definitely not using those. The log messages are just being written out to System.err.

So I'm not actually 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's Jettison and Joda, the MySQL driver, and then everything from Hibernate's required dependency directory and nothing else:

enter image description here

How do I increase the log level or otherwise disable SHOW WARNINGS in this (default settings) case? This mentions "log categories of interest" but I'm not sure how those relate to the configuration file. This page doesn't have any log related properties documented outside the "Logging" section, which mentions SLF4J, but apparently I'm not using SLF4J (how could I be, since it's not in my classpath).

like image 805
Jason C Avatar asked Oct 29 '22 07:10

Jason C


1 Answers

The hibernate framework enables the MySQL's SHOW WARNING by default with every query fired, this doubles the number of queries to MySQL and application can realise performance issues. This additional logging of SHOW WARNING by hibernate can be established at -

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

Solution

Make hibernate choose a proper logger. This can be done by adding : -Dorg.jboss.logging.provider=slf4j or -Dorg.jboss.logging.provider=log4j as a JVM runtime parameter.

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

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

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

log4j.logger.org.hibernate.type=ERROR
like image 169
Abhay Kumar Avatar answered Nov 15 '22 06:11

Abhay Kumar