Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order in closing resources

Should I close the statement before the connection? And the resultset before the statement? Or is it all the other way around?

Connection conn = null;
Statement st = null;
Resultset rs = null;

try {
    // Do stuff

} catch (SQLException e) {
    // Do stuff
}
finally {
    if (rs != null) rs.close();
    if (st != null) st.close();
    if (conn != null) conn.close();         
}

Or

Connection conn = null;
Statement st = null;
Resultset rs = null;

try {
    // Do stuff

} catch (SQLException e) {
    // Do stuff
}
finally {
    if (conn != null) conn.close();         
    if (st != null) st.close();
    if (rs != null) rs.close();
}
like image 383
user1883212 Avatar asked Dec 21 '22 05:12

user1883212


1 Answers

Close the result set, then the statement, then the connection.

In other words, close everything down on a last-in-first-out basis.

like image 92
Arthur Dent Avatar answered Jan 09 '23 16:01

Arthur Dent