Closing objects when not in use

DownloadDownload JDBC Driver

When you work with closable objects of Microsoft JDBC Driver for SQL Server, you should explicitly close them by using their close methods when they're no longer needed. This pattern particularly applies to SQLServerResultSet and the Statement objects like SQLServerStatement, SQLServerPreparedStatement, and SQLServerCallableStatement. Closing improves performance by freeing up driver and server resources quickly, instead of waiting for the Java Virtual Machine garbage collector to do it for you.

Closing objects is crucial to maintaining good concurrency on the server when you're using scroll locks. Scroll locks in the last accessed fetch buffer are held until the result set is closed. Similarly, statement prepared handles are held until the statement is closed. If you're reusing a connection for multiple statements, closing statements before they go out of scope allows the server to clean up the prepared handles earlier.

See also

Improving performance and reliability with the JDBC driver