Db connection leak

I am checking for Db connection leaks. I found following is standard way to close resources.

con = Connection.getConnection();

stat = con.createStatement()

rs = stat.executeQuery(sql);

finally

{

if (rs !=null) rs.close();

if (ss !=null) stat .close();

if (con !=null) co.close();

}

In some places I see following statements also. Here there is no reference for Statement object is created and hence it is not closed.

con = Connection.getConnection();

rs = con.createStatement().executeQuery(sql);

finally

{

if (rs !=null) rs.close();

if (con !=null) co.close();

}

As we are not closing Statement explicitly will it cause connection leak?

[1276 byte] By [Sheni_a] at [2007-11-26 19:56:04]
# 1
rememeber connection and statement both are different
rakesh_thakura at 2007-7-9 22:49:43 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
You mean to say second approach is wrong and I should split it?
Sheni_a at 2007-7-9 22:49:43 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> rememeber connection and statement both are different

Brilliant deduction. Too bad that it has diddly-jack-squat to do with the OP's question.

>You mean to say second approach is wrong and I should split it?

A Statement is closed when it is GC'd, but it is always a good practice to close it explicitly. I believe some JDBC drivers will close all open statements on a connection when the connection is closed, but it isn't part of the spec, so you can't rely on it. Note that explicit closing of a ResultSet is usually unnecessary, as a ResultSet is always closed when the Statement that created it is closed.

bckrispia at 2007-7-9 22:49:43 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
You appear to be closing the wrong statement.if (ss != null) stat .close();
dcmintera at 2007-7-9 22:49:43 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

> A Statement is closed when it is GC'd, but it is

> always a good practice to close it explicitly. I

> believe some JDBC drivers will close all open

> statements on a connection when the connection is

> closed, but it isn't part of the spec, so you can't

> rely on it. Note that explicit closing of a

> ResultSet is usually unnecessary, as a ResultSet is

> always closed when the Statement that created it is

> closed.

You can't rely on resource clean up in any case.

Always close the result set, statement and connection.

jschella at 2007-7-9 22:49:43 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

> In some places I see following statements also. Here

> there is no reference for Statement object is created

> and hence it is not closed.

>

> con = Connection.getConnection();

> rs = con.createStatement().executeQuery(sql);

>

That creates a statement. The fact that you don't have an explicit reference doesn't change the fact that it is created.

jschella at 2007-7-9 22:49:43 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...