PerfectDayToChaseTornados wrote:
You should try to close them in a finally block (checking first to see that
the connection is not null) this way you will be sure that they get closed.
How many multiple connections do you get on your server? If it is a busy
website you may well need in excess of 100 connections in the pool. We have
max connections set to 200 in ours. A good way to close your connections so
that you don't have to have too many nested connections is as follows:
try{
Connection con = myDataSource.getConnection();
try{
// database call code here
}
finally{
if(con != null){
con.close();
}
}
}
catch(SQLException sqle){
//handle exception here
}
Hmmm; don't you risk the possibility that an exception on the
con.close() will overshadow another exception and make debugging more
difficult?
I usually use the following pattern:
Connection conn = null;
try
{
conn = getConnection();
// use connection
}
catch (final SQLException sqle)
{
// handle exception; call conn.rollback() if desired
}
finally
{
try
{
if (conn != null)
{
conn.close();
}
}
catch (final SQLException sqle)
{
// log exception
}
}
The nesting is no deeper than the original code.
Actually, this kind of issue makes me wish that Java supported the
notion of attaching a finally block to a method. The syntax would look like:
public void doStuff()
{
// do stuff
}
finally
{
// clean up
}
That would make a lot of code look cleaner.
Ray