This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

I need advice on closing connection and stmt after use

 
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thank you for reading my post.
I want to know , how and where i should close statement ,PreparedStatement, Connection that i use in my source code.

imagine that i have a try catch block , so inside Try i create the connection , statement ,...
I do all my jobs here. if my source code execution face any exception it will goes to catch block , so it is very likely that i did not closed my connection ,....


if i try to close them i catch block , then i need another try catch for them

if i try to close them in finally block then i will need another try/catch block there.....

where and how i should close statement and connections ?
does closing statement close the connection too and vice-versa ?

in case that i use a connection pool , *must* i close a connection or it will be closed after i exit the scope(method) that i made the connection inside it ?


Thank you very much.
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by raminaa niilian:
i try to close them in finally block then i will need another try/catch block there.....


This is the one. You close them in a finally block that has a nested try/catch. That catch should just log the error message. You don't want your program to crash because there was a problem closing a resource.

in case that i use a connection pool , *must* i close a connection or it will be closed after i exit the scope(method) that i made the connection inside it ?


You still must close the connection (which really just returns it to the pool.) Otherwise, you will have a resource leak and the connection pool won't know about all the free connections.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One note about connections and pools. Connection creation is generally an expensive process, which is why they get pooled. Pools differ in many respects, but one of them is the way they deal with creating and destroying connections. Some will handle things more automatically then others, so you have to be careful about what you are using and how.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

does closing statement close the connection too and vice-versa ?


Closing a JDBC resource also closes any resource which it created. So, closing a Connection also closes the Statement (which will also close any ResultSet it may have created).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic