I have an JDBC utility class which hold the Resultset,Statement and Connection instances as global references.This class has basic two methods like 'execute'(has some parameters) and 'close' and inside execute, above instances are created and assigned and finally returns the created Resultset.
And the 'close' method closes all the opened connections for Resultset,Statement and Connections simply like this.
public void close() {
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
}
}
And my client application I create an instance of utility and get the resultset like below.
ResultSet rs = dbutil.execute(strQuery,values);
and if I close the Resultset using rs.close
and again using dbutil.close()
in my client application,
- will it occur some connection leak or memory leaks?
- what will happen if I use only
rs.close()
in my client? - Are there any possibilities for making connection leaks there?
thanks in advance
A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results.
Calling the method close on a ResultSet object that is already closed is a no-op. Please check the Java doc for resultset.