Usually I just close my connection once in the finally block, and I did not find any problems.
However, double closure may be required in more complex use cases. For example, if you open several statements or several connections in a try block, you want to close them immediately after using it. Then you need to catch any remaining part due to exceptions in the finally block. For instance,
try { conn = ds1.getConnection(); ... Do something with datasource 1 ... conn.close(); conn=null; conn = ds2.getConnection(); ... Do something with datasource 2 ... conn.close(); conn = null; } catch (SQLException e) { ... deal with errors ... } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { ; } conn = null; } }
source share