Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

should close connection or close session?

 
raj chopra
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hii All,

my project uses jdbc and hibernate while executing queries. most of time connections,session,resultsets and prepared statements,callable statements were not closed.
This leads lo resource management issues and i tend to close the connections. so i added the try catch finally block and modified the code to close the connections

In this regard i have a dialama in closing the connections or the sessions? which would be better and safe practise?

sample code would be

 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Connection is the way to go.

Assuming it's web app, this Session (HttpSession) will get invalidated when the user logs out or when the app idles too long and timeout. Closing such session after each database transaction can have what effect on your app?
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to me what would be better (in the normal sort of application) would be to tell Hibernate where your connection pool is, and let Hibernate manage that. Then use Hibernate transactions to manage your database transactions, and use Hibernate sessions according to one of the preferred ways described in the Hibernate documentation.

Trying to manage your own database connections seems to me to be a recipe for confusion, and indeed that seems to be what is happening.
 
raj chopra
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Tsang,

Yes my project is web application. i am not sure on the effets of closing session in my app, because the code is inconsistent . on few areas code involved hibernate sessions and few orther areas with simple jdbc connections.on trial and error basis i closed few connections last time and cross cheked in tomcat server cmd prompt. it was ok on few classes, and connection closed out exception was result on orther class files.

with your suggestions i prefer to close the connection, as my primary idea is to close the database connection once the query is performed.

hope i am going in right path.
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is unusual to use both JDBC and Hibernate together in different classes. I suggest you use Hibernate or JDBC throughout. If using Hibernate, like Paul said let the Hibernate code handle the connection pool and stuff. If JDBC you should close the connection after each call.
 
raj chopra
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes the code is incosistant that's the problem i am dealing with.

could you suggest me how do i proceed in closing the connection in below file



i kind of tried the closing the connection in this way , however was facing with sql eception like connection closed out .

the modified code for closing connection to above file was


>
 
raj chopra
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any suggestion in resolving this will be greatly helpful.

LOG from tomcat :




code related to this error was
>
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's really hard to follow all that code.

Your print stack error is NullPointerException somewhere. But it's hard to identify which try block caused this exception since you got 4 sets A,B,C,D.

If the line number 502 is indicator, it's in try block C but not sure.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic