Announcement Announcement Module
Collapse
No announcement yet.
When to close sessions and when not to (OpenSessionInView) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • When to close sessions and when not to (OpenSessionInView)

    I am using OpenSessionInView and using Spring+Hibernate together, to build a webapp.

    I notice that I'm getting a steady stream of WARNs in the log about unclosed connections. At the same time, I'm confused as to how much (or little) Spring and the HibernateTemplate is helping me with closing sessions after I use them.

    This is especially true after I tried session.disconnect() after doing queries in my DAOs and getting a bunch of session closed errors.

    When should I be closing sessions, and when should I let Spring and OpenSessionInView handle it for me??

    Andrew
    Seattle, WA

  • #2
    And a potentially related problem...

    After a while, the queries will hang:

    Code:
    2005-05-10 00&#58;05&#58;32,163 DEBUG &#91;net.sf.hibernate.hql.QueryTranslator&#93; - <HQL&#58; from org.phatcast.db.Member as member where member.username = ?>
    2005-05-10 00&#58;05&#58;32,163 DEBUG &#91;net.sf.hibernate.hql.QueryTranslator&#93; - <SQL&#58; select member0_.id as id, member0_.USERNAME as USERNAME, member0_.PASSWORD as PASSWORD, member0_.EMAIL as EMAIL, member0_.INTEREST_STRING as INTEREST5_, member0_.PROFILE as PROFILE, member0_.WEBSITEURL as WEBSITEURL, member0_.PICTUREURL as PICTUREURL, member0_.STARTDATE as STARTDATE, member0_.IS_ACTIVE as IS_ACTIVE, member0_.IS_CONFIRMED_EMAIL as IS_CONF11_, member0_.BALANCE as BALANCE from MEMBER member0_ where &#40;member0_.USERNAME=? &#41;>
    2005-05-10 00&#58;05&#58;32,173 DEBUG &#91;net.sf.hibernate.impl.BatcherImpl&#93; - <about to open&#58; 0 open PreparedStatements, 0 open ResultSets>
    ... and just sits there.

    The WARN messages I get about the sessions look like:

    Code:
    2005-05-10 00&#58;01&#58;54,160 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - <running Session.finalize&#40;&#41;>
    2005-05-10 00&#58;01&#58;54,160 DEBUG &#91;net.sf.hibernate.impl.SessionImpl&#93; - <running Session.finalize&#40;&#41;>
    2005-05-10 00&#58;01&#58;54,160 WARN &#91;net.sf.hibernate.impl.SessionImpl&#93; - <unclosed connection, forgot to call close&#40;&#41; on your session?>
    I really need to solve the hanging query problem, and I'm thinking the unclosed sessions have something to do with it.

    Any thoughts or suggestions on how to investigate this?

    Comment


    • #3
      Each session should be properly closed. Warn gets stated when session is picked up by garbage collector and is closed on finalize.
      If using disconnect architecture, I think OSIF is not the thing to use.

      Comment


      • #4
        Originally posted by alesj
        Each session should be properly closed. Warn gets stated when session is picked up by garbage collector and is closed on finalize.
        If using disconnect architecture, I think OSIF is not the thing to use.
        Where should I be closing these sessions?

        1) Should each method within the DAOs call this.closeIfNecessary?

        2) Or should this be happening in the controllers?

        And to confirm, is the error I'm seeing with the hung queries caused by having too many sessions open?

        Comment


        • #5
          1) Should each method within the DAOs call this.closeIfNecessary?

          I would use HibernateTransactionManger to handle my session.

          2) Or should this be happening in the controllers?

          Defenitely no. Controllers should be unaware of session - decoupling app layers.

          3) And to confirm, is the error I'm seeing with the hung queries caused by having too many sessions open?

          No idea why this happens.

          Comment

          Working...
          X