Class StatementFinalizer

All Implemented Interfaces:
InvocationHandler

public class StatementFinalizer extends AbstractCreateStatementInterceptor
Keeps track of statements associated with a connection and invokes close upon Connection.close() Useful for applications that don't close the associated statements after being done with a connection.

A statement is tracked with a strong reference until it is closed: an application that forgot about a statement is exactly the case the interceptor exists for, so finalisation must not depend on that statement still being reachable when the connection is returned. Statements that the application does close are forgotten by the returned proxy at the moment close() completes, keeping the tracked list bounded by the statements that are genuinely still open and closed through that handle. (The removal cannot observe a close that reaches the statement by another route: an interceptor placed outside this one may swallow the statement's close(), e.g. a statement cache recycling instead of closing, and a statement the application closes through the driver handle that unwrap hands back never passes through this proxy; such entries are simply reclaimed at the connection return, where the renewed close meets a statement already closed as a harmless no-op, as they were before.)

Tracked statements are handed to the application as proxies implementing the standard JDBC statement interfaces, as is the statement facade the pool always installs in front of this interceptor and as are the proxies of the other statement-wrapping interceptors; the driver's own statement class is therefore not reachable by casting. The Wrapper methods are delegated to the driver, and unwrap is the supported way to reach driver-specific statement methods. A statement the application reaches that way is still tracked here and is closed, like any other tracked statement, when the connection is returned - unless an interceptor listed after this one recycles the statement instead of closing it, as a statement cache beneath this interceptor does.