-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate stat logging shows tons of empty sessions??
PostPosted: Tue Jun 20, 2017 11:37 am 
Newbie

Joined: Tue Jun 20, 2017 11:31 am
Posts: 2
Hi there,

I am using Hibernate 5 with Spring API. Running Java 8 with apache tomcat9. Mysql 5.x
I access objects using getSession().getCurrentSession(), and only have one spring session per request. (happy to paste my struts action, but not sure if it would help).

I enabled stat logging, and it displays all the queries, and session statistics at the end.
So I would see a bunch of queries and then this:
Code:
2017-06-17 19:04:56,621 INFO  org.hibernate.engine.internal.StatisticalLoggingSessionEventListener.end - Session Metrics {
    1591683 nanoseconds spent acquiring 9 JDBC connections;
    1073951 nanoseconds spent releasing 8 JDBC connections;
    1173965 nanoseconds spent preparing 10 JDBC statements;
    6098889 nanoseconds spent executing 10 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;
    100987632 nanoseconds spent executing 7 flushes (flushing a total of 413 entities and 1445 collections);
    95887194 nanoseconds spent executing 3 partial-flushes (flushing a total of 130 entities and 130 collections)
}


However, after this (session is ended), it also shows about 20-40 empty sessions like this:
Code:
2017-06-17 19:04:56,873 INFO  org.hibernate.engine.internal.StatisticalLoggingSessionEventListener.end - Session Metrics {
    0 nanoseconds spent acquiring 0 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)
}



Does anyone know if this is normal?


Top
 Profile  
 
 Post subject: Re: Hibernate stat logging shows tons of empty sessions??
PostPosted: Tue Jun 20, 2017 2:23 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I checked the code and it looks just fine in Hibernate.

Are you using Open Session in View? If you are using Spring Boot, there's a good chance you are already using it.

Open Session in View would explain the empty sessions that get opened/closed without doing any DB work since you might have some web actions that don't end up in the DAO layer.


Top
 Profile  
 
 Post subject: Re: Hibernate stat logging shows tons of empty sessions??
PostPosted: Tue Jun 20, 2017 6:19 pm 
Newbie

Joined: Tue Jun 20, 2017 11:31 am
Posts: 2
Hi there, thank you for your reply.

I am using OpenSessionInView. I haven't seen spring boot... it may be used by default.

It's crazy to think there are 20 web actions that open spring sessions with no DB work (with one true web request).

Anyways, thanks for your reply! As long as this isn't going to impact performance. Would love to know how to decrease them to only the session that does DB work. (I am using Struts 2.0 with DispatchAction). I tried decreasing database transactions to see if it will help - it decreases the #of statements in the active session, but the 20 extra empty sessions still show up.

Cheers


Web.xml
Code:
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
   
   <filter> <!-- Get spring to keep the session open for the whole request, so hibernate's lazy loads work -->
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
    <init-param>
         <param-name>sessionFactoryBeanName</param-name>
         <param-value>sessionFactory</param-value>
     </init-param>
  </filter>
  <filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
  </filter-mapping>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>/WEB-INF/applicationContext.xml</param-value>
       </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
   
    <!-- servlet>
       <servlet-name>mvc</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <load-on-startup>3</load-on-startup>
    </servlet-->

    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
   
    <servlet>
        <description/>
        <display-name>UploadServlet</display-name>
        <servlet-name>UploadServlet</servlet-name>
        <servlet-class>org.imagequiz.web.UploadServlet</servlet-class>
    </servlet>
   
    <servlet-mapping>
        <servlet-name>UploadServlet</servlet-name>
        <url-pattern>/admin/upload</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>UploadServlet</servlet-name>
        <url-pattern>/image</url-pattern>
    </servlet-mapping>
    <!-- servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping-->
    <!-- >servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping-->
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
       <servlet-name>dispatcher</servlet-name>
       <url-pattern>*.me</url-pattern>
    </servlet-mapping>


Top
 Profile  
 
 Post subject: Re: Hibernate stat logging shows tons of empty sessions??
PostPosted: Wed Jun 21, 2017 1:23 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
It's surely something in your Session management code since, from a Hibernate perspective, it's just a matter of calling Session.close to trigger the statistics report.

Make sure you add the thread id to your logging pattern, and you log the web action that gets dispatched too. This way, you can figure out what web action triggers those 20 empty Session calls. Or maybe it's a batch processor. You can't know unless you log the thread id and aggregate the logs.

Quote:
I am using OpenSessionInView.


That's bad for performance. You shouldn't use it since it's an Anti-Pattern.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.