Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does Hibernate print statistics?

Tags:

java

hibernate

I've recently integrated hibernate into my webapp and am trying to see the performance impact/frequency of db calls that are happening.

After enabling show_sql and generate_statistics, when I run the application I see the sql queries run by hibernate and also hibernate statistics. Eg:

08:04:53.724 [http-apr-8080-exec-1] INFO  o.h.e.i.StatisticalLoggingSessionEventListener - Session Metrics {
85648 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
0 nanoseconds spent preparing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}

However the statistics statement gets printed multiple times per api call while the query statements are much lower in number. Also, its very rare to see a non zero value for any other metric except acquiring 1 JDBC connections in Statistics logs.

So when exactly does the logger run and what am I doing wrong to get so many metric logs?

Thanks

like image 662
sudshekhar Avatar asked Jun 12 '17 08:06

sudshekhar


3 Answers

When you enable hibernate statistics you get session statistics information every time a session is closed. if you dont want this behavior you can disable it with adding of following entry in your log4j file:

log4j.logger.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=OFF
like image 94
Abass A Avatar answered Nov 03 '22 21:11

Abass A


If you don't want to get to as deep as session events, you can disable them from getting logged by using this property "hibernate.session.events.log=false". (You may do it with your log4j configuration also, based on your needs).

It appears that since hibernate 4, if you enable "hibernate.generate_statistics", the session events are also logged by default. But the logs will get filled up heavily as the events are logged per session. So, use when you need to analyze any performance issues locally.

More details on enabling/disabling the event logs here: https://hibernate.atlassian.net/browse/HHH-8793

like image 45
Chaithu Narayana Avatar answered Nov 03 '22 20:11

Chaithu Narayana


It helped me:

spring:
  jpa:
    properties:
      generate_statistics: false

in application.yml

like image 40
Alexander Lu Avatar answered Nov 03 '22 20:11

Alexander Lu