-->
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.  [ 5 posts ] 
Author Message
 Post subject: WARN SessionImpl unclosed connection and pgm dies
PostPosted: Fri Dec 08, 2006 5:25 pm 
Newbie

Joined: Fri Dec 08, 2006 4:55 pm
Posts: 3
Using Hibernate 2 and MySql 4.1.20

I have a program that ran constantly (and successfully) for a month when it suddenly just died with no exception info and no stack trace (we print a stack trace when a catchable exception is thrown).
The last thing that we see in the log file is the following error:
WARN [07 Dec 2006 02:11:18] (SessionImpl) unclosed connection

The program runs in a thread which mostly sleeps. It wakes up every minute, does a database call that checks for a particular status on a row in a table and then, if it finds the appropriate status (normally 1/hour), it runs through code which reads our database and writes out some info to a flat file. As mentioned above -- it ran for nearly a full month without any errors or problems.

The program gets one connection upon startup. Then it is in this infinite loop which uses this connection (passing it to the class which does the database reads). At the end of each processing cycle (time through the loop) it does a conn.commit();

There is a finally block which does close the connection -- though it shouldn't really ever hit this as it doesn't really ever stop unless the process is killed.

So basically the code looks like this:
Code:
  Connection conn = null;
      try {
              conn = HibernateUtil.getSession().connection();
      do some initialization which includes a few database calls;
      while(true) {
         get an indicator from the database.
         if (indicator set to true) {
               do some database reads and write stuff to a flat file
               SECONDS.sleep(60);
               conn.commit();
      }
   } catch (Exception e)
      e.printStackTrace();
   } finally {
      if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e3) {
                    e3.printStackTrace();
                    log.error("could not close connection! "+e3);
                }
            }   


Here is the code for getting the Hibernate Session:
Code:
public static Session getSession() throws HibernateException {
        Session s = (Session) threadSession.get();
        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            threadSession.set(s);
        }
        return s;
    }


Here are the c3p0 settings in my hibernate.cfg file.

<!-- See http://www.hibernate.org/214.html -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">30</property>
<property name="c3p0.timeout">30</property>


Any ideas on what may be causing this thread to die?

_________________
Helen
www.gather.com


Top
 Profile  
 
 Post subject: Re: WARN SessionImpl unclosed connection and pgm dies
PostPosted: Fri Dec 08, 2006 6:22 pm 
Newbie

Joined: Fri Dec 08, 2006 5:59 pm
Posts: 1
I'm afraid I have more questions than answers.

Can you provide more information about what's in "do some database reads and write stuff to a flat file"? It seems like that's more likely to be throwing some kind of exception (or Error, possibly, which wouldn't be caught in that catch block, though I'd still expect that to get dumped out in a log)

Without arguments, printStackTrace() just drops the text to stderr. Are you sure that the catch block below that logs to a point that you can read?

What does the database look like? Are there open connections from the JVM to the database? Is the JVM even still running?

Also, do you have some reason for calling conn.commit() directly, as opposed to using the Hibernate Session.beginTransaction() method? I don't know that that would cause termination of your program, but it seems a little questionable to me.


Top
 Profile  
 
 Post subject: WARN SessionImpl unclosed connection and pgm dies
PostPosted: Mon Dec 11, 2006 11:32 am 
Newbie

Joined: Fri Dec 08, 2006 4:55 pm
Posts: 3
I have verified with a test that if an exception is thrown (in the code that does the database reads and writes to a flat file) that it writes the exception to the log file. In addition, after we issue the print stacktrace, we send an email/page to a list of addresses (and with my test -- which was to force an exception, I received the email).

I have also verified that the thread methods always throw the exceptions so they should end up caught in the main try-catch block detailed above.

When this program is started it gets a new database connection which it then uses forever. The c3p0 pooling options are set to actually create 5 connections to the database but it only is using 1 of them. I ran the program for several hours on my local machine and verified that it only uses/gets the 5 connections (i.e. -- not getting another new connection after it starts).

Yes, when the thread dies the JVM is still running. There is actually a parent thread that starts 2 child threads. One of the child threads is the one that is dying -- again, after running constantly for over 30 days. While the one thread died, the other kept running okay. The other thread has its own database connection. Again it gets one for the thread and uses it forever.

I was issuing the commit to make sure that the database was updated at the end of each cycle. I found that if I didn't do that that when I started at the top of the loop to get the indicator -- it would have old data.

We have restarted the program in our production environment and it has been running fine for the past 5 days. But I am worried that in another 25 days or so, the thread will die again.

_________________
Helen
www.gather.com


Top
 Profile  
 
 Post subject: WARN SessionImpl unclosed connection
PostPosted: Thu Dec 14, 2006 4:16 pm 
Newbie

Joined: Fri Dec 08, 2006 4:55 pm
Posts: 3
I believe I figured out the problem (though not 100% sure).
I think that the program is throwing an Error (a RuntimeException such as OutOfMemoryError). This would not be caught. And the result would be an unclosed connection.

I figured this out because I was running a junit test (with hibernate calls) within Eclipse and hit an Out-of-heapspace error (shown in the Junit tab) and noticed the warning message about an unclosed connection on my console.

_________________
Helen
www.gather.com


Top
 Profile  
 
 Post subject: Re: WARN SessionImpl unclosed connection
PostPosted: Thu Dec 14, 2006 8:55 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
HelenFeder wrote:
I believe I figured out the problem (though not 100% sure).
I think that the program is throwing an Error (a RuntimeException such as OutOfMemoryError). This would not be caught. And the result would be an unclosed connection.

I figured this out because I was running a junit test (with hibernate calls) within Eclipse and hit an Out-of-heapspace error (shown in the Junit tab) and noticed the warning message about an unclosed connection on my console.


i am sure u are aware of this but . pointing it out to just in case

http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#connection()

If the session is using aggressive collection release (as in a CMT environment), it is the application's responsibility to close the connection returned by this call. Otherwise, the application should not close the connection.

_________________
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.