Search code examples
javaintellij-ideaprepared-statementresultsetinspection

IntelliJ inspections for unclosed PreparedStatements/ResultSets/Connections


Are there any IntelliJ inspections for unclosed PreparedStatements/ResultSets/Connections or anything using the standard Java SQL libraries?

I have to work with a relatively old Java codebase that uses a lot of SQL that isn't wrapped in a framework, so remembering all the special closing logic in the finally blocks for even very small bits of SQL can become tiresome and error-prone.


Solution

  • Be sure to turn on the following inspection: Settings > Editor > Inspections > Java > Resource management issues > "JDBC resource opened but not safely closed"

    Reports any JDBC resource which is not safely closed in a finally block. Such resources may be inadvertently leaked if an exception is thrown before the resource is closed. JDBC resources reported by this inspection include java.sql.Connection, java.sql.Statement, java.sql.PreparedStatement, java.sql.CallableStatement, and java.sql.ResultSet.

    Please note that you can search the inspections list to find ones you need.