while it has been discussed many times, to close the database resources in shortest scope possible. I would like to know if its okay to reuse the statement objects, which does not return any result set.
forexample a code like this,
Statement st = con.getStatement();
st.execute(TABLE1_DELETE_Query);
st.execute(TABLE2_DELETE_Query);
st.close();
I looked at similar questions on SF, yet I was unable to find that could have answered mine. If it has been answered, kindly just mention the reference.
Thanks
The lifetime of Statements should be kept short term, but re-using them is perfectly fine.
A single unclosed ResultSet can cause an application to hang, but you can have 100s of unclosed statements without a problem. Typically a statement will be holding on to a very small piece of memory associated with the database connection and little else, but it can hold multiple result sets as could be returned by the getMoreResults() method, warnings for the getWarnings() method.
Reusing a Statement many times within a single method and then closing it is good practice as it saves needlessly creating extra Statements. Creating a Statement and hanging on to it just in case it is wanted is bad practice.
PreparedStatements are intended to be re-used, but it is still best to keep them within the scope of a single method, as that guarantees they have a short term life-span.