Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

closing the database connection

Ranch Hand
Posts: 644
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

In my code, I have made sure that I am closing all the open dataBase connections like below:


where conn is the connection object. and null is for preparedStatement.

In the dao close() method if connection is not null, con.close() (java method) is called.
While debugging the application , I found out that the connection is not getting close.

my question is : does the connection is closed by calling connection.close method (provied by jdk) or it just marks it for garbage collection.

How can I make sure that the connection is getting closed? (is it by passing exclusively null for connection object?)

[ April 19, 2005: Message edited by: trupti nigam ]
author & internet detective
Posts: 41086
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using a connection pool, close() returns the connection to the pool. If you are not using a connection pool, close() closes the connection.

close() does not null out the reference to the connection object in your code. If you try to use the connection, you will get a StaleConnectionException. However, having this reference does not affect the connection resources.

Why aren't you closing the PreparedStatement too?
Are we home yet? Wait, did we forget the tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
    Bookmark Topic Watch Topic
  • New Topic