I am trying to find out what is the best approach for try/catch when dealing with connections in JDBC.
My personal view is.
Connection conn = null;
try{
conn = getDBConnection();
//Some jdbc code
} catch(SQLException e) {
//...
} finally {
if(conn != null) {
try {
conn.close();
} catch(SQLException e) {
//....
}
}
}
But I have seen few smart guys doing this
Connection conn = null;
try{
conn = getDBConnection();
//Some jdbc code
conn.close();
conn = null;
} catch(SQLException e) {
//...
} finally {
if(conn != null) {
try {
conn.close();
} catch(SQLException e) {
//....
}
}
}
Is there any advantage in closing the connection in try and setting it to null?