Skip to content

Latest commit

 

History

History
21 lines (16 loc) · 1.71 KB

closing-objects-when-not-in-use.md

File metadata and controls

21 lines (16 loc) · 1.71 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic
Closing objects when not in use
In JDBC programming, it's important to close objects when the aren't in use. Closing improves performance and frees up client and server resources quickly.
David-Engel
v-davidengel
04/20/2021
sql
connectivity
conceptual

Closing objects when not in use

[!INCLUDEDriver_JDBC_Download]

When you work with closable objects of [!INCLUDEjdbcNoVersion], 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