File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes closing the database connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "closing the database connection" Watch "closing the database connection" New topic

closing the database connection

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
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 ]
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32815

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?

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
It is sorta covered in the JavaRanch Style Guide.
subject: closing the database connection
It's not a secret anymore!