How to Correctly Close Resources

前端 未结 6 1434
孤独总比滥情好
孤独总比滥情好 2020-12-09 16:48

As I was cleaning up some code, FindBugs pointed me to a bit of JDBC code that uses Connection, CallableStatement, and ResultSet objects. Here\'s a snippet from that code:

6条回答
  •  温柔的废话
    2020-12-09 17:13

    You only have to close the Connection.

    try
    {
        cStmt.close();
    }
    catch(Exception e)
    {
        /* Intentionally Swallow Exception */
    } 
    

    From docs.oracle.com:

    A Statement object is automatically closed when it is garbage collected. When a Statement object is closed, its current ResultSet object, if one exists, is also closed.

    Calling close() on a Connection releases its database and JDBC resources.

提交回复
热议问题