Connection's Close method, should it be called?  
Author Message
MS





PostPosted: 2005-2-2 2:07:00 Top

java-programmer, Connection's Close method, should it be called? Hi,

I am unsure whether it is good Java practise to use Close() to close a
database Connection.

EG:

dbConnection = DriverManager.getConnection(url, user, pass);
Statement sqlStatement = dbConnection.createStatement...
String sql = "SELECT ...
ResultSet results = sqlStatement.executeQuery(sql);
...do stuff with results...

Now that I've finished should I call close() or not?
dbConnection.close();

The API documentation makes it clear it will be closed automatically when
the garbage is collected -- but is it good practise to close it 'manually'
when you've finished with it?

Thanks,

MS

API Doc:

void close()
throws SQLException

Releases this Connection object's database and JDBC resources immediately
instead of waiting for them to be automatically released.

Calling the method close on a Connection object that is already closed is
a no-op.

Note: A Connection object is automatically closed when it is garbage
collected. Certain fatal errors also close a Connection object.

Throws: SQLException - if a database access error occurs

 
Joe Weinstein





PostPosted: 2005-2-2 2:30:00 Top

java-programmer >> Connection's Close method, should it be called?

MS wrote:

> Hi,
>
> I am unsure whether it is good Java practise to use Close() to close a
> database Connection.
>
> EG:
>
> dbConnection = DriverManager.getConnection(url, user, pass);
> Statement sqlStatement = dbConnection.createStatement...
> String sql = "SELECT ...
> ResultSet results = sqlStatement.executeQuery(sql);
> ...do stuff with results...
>
> Now that I've finished should I call close() or not?
> dbConnection.close();
>

Hi. It is definitely better to close JDBC resources ASAP. It releases
DBMS resources as well as driver and JVM resources. Here's what works
well:

myJDBCMethod()
{
Connection c = null; // define as a method-level variable
// for thread-safety
try {
c = DriverManager.getConnection(...); // establish in try
...do all jdbc...
c.close();
c = null;
}
catch (Exception e) {
... do whatever... If a catch block isn't wanted,
.... just have a try-finally
}
finally {
// for safety make sure connection is closed.
// do every step in a finally block in it's own
// try-catch-ignore so every step is attempted
if (c != null) try {c.close();} catch (Exception ignore){}
}
}

Joe Weinstein at BEA

> The API documentation makes it clear it will be closed automatically
> when the garbage is collected -- but is it good practise to close it
> 'manually' when you've finished with it?
>
> Thanks,
>
> MS
>
> API Doc:
>
> void close()
> throws SQLException
>
> Releases this Connection object's database and JDBC resources
> immediately instead of waiting for them to be automatically released.
>
> Calling the method close on a Connection object that is already closed
> is a no-op.
>
> Note: A Connection object is automatically closed when it is garbage
> collected. Certain fatal errors also close a Connection object.
>
> Throws: SQLException - if a database access error occurs
>

 
MS





PostPosted: 2005-2-2 2:45:00 Top

java-programmer >> Connection's Close method, should it be called? Joe Weinstein emailed this:
> MS wrote:
>
>> Hi,
>>
>> I am unsure whether it is good Java practise to use Close() to close a
>> database Connection.
>>
>> EG:
>>
>> dbConnection = DriverManager.getConnection(url, user, pass);
>> Statement sqlStatement = dbConnection.createStatement...
>> String sql = "SELECT ...
>> ResultSet results = sqlStatement.executeQuery(sql);
>> ...do stuff with results...
>>
>> Now that I've finished should I call close() or not?
>> dbConnection.close();
>>
>
> Hi. It is definitely better to close JDBC resources ASAP. It releases
> DBMS resources as well as driver and JVM resources. Here's what works
> well:
>
> myJDBCMethod()
> {
> Connection c = null; // define as a method-level variable
> // for thread-safety
> try {
> c = DriverManager.getConnection(...); // establish in try
> ...do all jdbc...
> c.close();
> c = null;
> }
> catch (Exception e) {
> ... do whatever... If a catch block isn't wanted,
> .... just have a try-finally
> }
> finally {
> // for safety make sure connection is closed.
> // do every step in a finally block in it's own
> // try-catch-ignore so every step is attempted
> if (c != null) try {c.close();} catch (Exception ignore){}
> }
> }
>
> Joe Weinstein at BEA

Thanks Joe. I've put it in the finally clause as you suggest.

MS