Search code examples
javajdbcconnectionprepared-statementresultset

JDBC- Will the Prepared Statement instantiated in try block and not accessible in finally block be closed once Conn is closed in finally block


It is said that when we close connection object, the preparedstatement, resultset everything gets closed. In existing project code I found that PreparedStatement is not closed explicitly and also it is instantiated in try block and is not accessible in finally block where we have closed the Connection object. So will the rule that closing connection object will close the preparedstatement as well still applies here?

Help will be appreciated :)

Note- I do know that closing Preparedstatement explicitly is good practice from clean up point of view.

We have code where preparedStatement is not closed explicitly. PreparedStatement is instantiated in try block and is not accessible in finally block. We are closing connection object in finally block. ideally we expect all resources to get closed once connection object is closed. But since preparedstatement is not even accessible in finally block, will the rule still remains same?


Solution

  • I found that PreparedStatement is not closed explicitly and also it is instantiated in try block and is not accessible in finally block where we have closed the Connection object. So will the rule that closing connection object will close the preparedstatement as well still applies here?

    The prepared statement in the database will be cleaned up no later than when the database connection with which it is associated is closed. This is a matter of the database's own resource management.

    Just like any other object, the PreparedStatement instance in the Java VM will be subject to garbage collection when it ceases to be accessible from any live thread. This is ordinary Java memory management.

    But whether the close() method of the PreparedStatement will ever be invoked under the circumstances you describe is a function of the JDBC driver providing the Connection and PreparedStatement implementations. It is possible, for example, that the Connection maintains references to the PreparedStatement objects associated with it, and ensures that their close() methods are invoked when it is itself closed. It is also possible that the PreparedStatement implementation has a finalizer or a reference-queue-based cleanup mechanism that ensures that its close() method is invoked. But without something along these lines, no, the close() method will not be invoked if the reference is lost or abandoned first.

    We have code where preparedStatement is not closed explicitly. PreparedStatement is instantiated in try block and is not accessible in finally block. We are closing connection object in finally block. ideally we expect all resources to get closed once connection object is closed. But since preparedstatement is not even accessible in finally block, will the rule still remains same?

    To the extent that what you describe is a rule that you can rely on, as opposed to one that you are obligated to obey, yes, you can still rely on it despite there not being a reference to the PreparedStatement accessible in the finally block where the connection is closed. Having an accessible reference matters only if you want to do something that actually uses that reference. That doesn't apply here because you are not explicitly closing the PS (or doing anything else with its reference) anyway.

    Overall, you should not be much concerned about a resource leak in the situation described. But if there were one, then that would best be characterized as a bug in the JDBC driver, not in your code, though that distinction might be lost on your application's users.