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 |
[!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.