-->
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.  [ 3 posts ] 
Author Message
 Post subject: SQL and Hibernate: Where/how do I close the Connection?
PostPosted: Mon Feb 27, 2006 7:46 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
Hibernate 2

Basically, what I'm trying to do is something like this (this is just an example, please don't read into it beyond the connection issues):

Code:
//open session from factory
Object o = session.find( finderHql );

connection = session.connection();
statement = connection.createStatement();
results = statement.executeQuery( reportSql );

//process report results
//close Statement and ResultSet in finally


Now I'm here with a Connection object and a Session. If I close the connection with connection.close(), Hibernate will bonk on session.close() or if I try to load/update any more Hibernate objects. If I DON'T close the connection manually, it remains open after I call session.close() and hangs the app...

Does anyone know a way around this? I'd like to be able to check for an open connection and make closing decisions. the session.connection object is null throughout the Session's life unless (and until) session.connection() is called...Then that connection doesn't seem to close after the session is closed...

Also, session.isConnected() returns false even after connection.close() is called.


Last edited by Tremelune on Tue Feb 28, 2006 1:16 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 12:36 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Don't close the connection. Close any statements/result sets you get, then just forget the connection. Hibernate (or your configured connection manager) looks after that for you.

Note that you cannot guarantee that the connection will remain open after you've closed the session. Perhaps your app is closing because you're continuing to use a result set after you've closed your session? That'll just hang until your request timeout expires.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 1:16 am 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
Great, thank you. There was debate over whether or not session.close() handled connections...My personal debugging showed the connection's boolean "isOpen" value to be true after session.close() was called and the javadocs are a bit ambiguous on the subject.


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