Closing a Database Connection

Once an application is finished using an open connection, that connection should be closed. Some APIs automatically close connections when the application exits; however, it is best to explicitly close the connection as soon as the application is finished accessing the database to allow other applications access to precious connection resources.

Tip

Calling the appropriate close methods when finished with an ADO.NET or JDBC object is essential to freeing precious database resources used by the application. Both ADO.NET and Java offer very few guarantees when objects are finalized; therefore, it is common for those applications that don’t close objects explicitly to run out of database resources (such as connections, statement handles, and memory).

Get SQL in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.