Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

should close connection or close session?

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 27276
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic