-->
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: Should I close connection or Session if I use s.connection?
PostPosted: Wed Jan 04, 2006 10:10 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

2.1.7

Hi everyone:

I get a connection from a hibernate session in my application,the code is the following :

Code:
Session session=HibernateUtil.currentSession();
Connection con=session.connection(); <----- Should I close it myself?

Statement stm=con.createStatement(); <----- Should I close it myself?

ResultSet rs=stm.executeQuery(hsql);  <----- Should I close it myself?

close what???


I want to know after I use the resultSet ,Should I close the connection or close Session? I mean that if I don't need have to close jdbc connection and statement after I close Hibernate Session?
I don't know the following code which is right ?
1.
Code:
Session session=..... getSession();
Connection con=session.connection();
Statement stm=con.createStatement();
ResultSet rs=stm.executeQuery(hsql);
session.close();   <---------------Close Hibernate Session only ?

Or
2.
Code:
Session session=..... getSession();
Connection con=session.connection();
Statement stm=con.createStatement();
ResultSet rs=stm.executeQuery(hsql);
............do some jdbc work here ................
rs.close();     <----------close ResultSet myself?
stm.close();  <----------close Statement
con.close();  <----------close Connection



I want to know which is right in above two method? Thks!

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 11:27 am 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
Do not close the connection if you are using connection pooling, when you close the session hibernate will close the connection even if you are creating your own Statement. Be sure not to close the session until you and finished with the resultset.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 11:36 am 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
sorry, you should...

// if you want to use hql
Session s = getSession;
Query q = s.createQuery("from myEntity where column1 = :value");
q.setString("value", value);
q.uniqueResult(); or q.list();

// if you want the mappings and use straight sql
Session s = getSession;
Connection c = s.getConnection();
Statement ste = c.createStatement();
ResultSet rs = ste.execute(QueryStr);

while(rs.next()) {
// process results...
}

s.close();

Unless you really need to do something special use the first example.


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.