ADO - ADO Connection Close Method and Object Cleanup
The ADO Connection Close method is used to terminate an active connection between an application and a database. When an application finishes performing database operations, it should close the connection properly. Closing the connection releases the resources being used by the database connection and helps maintain application performance.
1. What Is an ADO Connection?
In ADO (ActiveX Data Objects), the Connection object represents a connection between an application and a data source such as SQL Server, Microsoft Access, or another database system.
Before an application can execute database operations, it generally creates a Connection object and opens it using an appropriate connection string.
For example:
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=SQLOLEDB;Data Source=ServerName;Initial Catalog=StudentDB;Integrated Security=SSPI;"
conn.Open
After conn.Open is executed, the application has an active connection to the database.
When the database work is completed, the connection should be closed.
2. The Close Method
The Close method terminates an open ADO connection.
The basic syntax is:
connection.Close
For example:
conn.Close
This tells ADO that the application no longer needs the active database connection.
Closing the connection does not necessarily destroy the Connection object itself. The object can still exist in memory and can potentially be opened again if it is still valid.
3. Why Should a Connection Be Closed?
A database connection consumes resources. While a connection is open, resources may be allocated by both the application and the database server.
Keeping unnecessary connections open can cause several problems:
-
Increased memory usage
-
Increased database-server resource consumption
-
Reduced availability of database connections
-
Slower application performance
-
Connection-pool exhaustion in applications using connection pooling
-
Difficulty handling a large number of simultaneous users
Therefore, an application should close a connection as soon as it has completed the database operations that require that connection.
4. Closing a Connection After Database Operations
A common pattern is to open a connection, perform the required operation, and then close it.
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=SQLOLEDB;Data Source=ServerName;Initial Catalog=StudentDB;Integrated Security=SSPI;"
conn.Open
' Perform database operations here
conn.Close
Set conn = Nothing
Here, two different operations are performed:
conn.Close
closes the database connection.
Set conn = Nothing
removes the application's reference to the Connection object.
These operations serve different purposes.
5. Close Versus Set Nothing
This distinction is important when learning ADO.
Close affects the database connection.
conn.Close
It changes the state of the connection from open to closed.
Set conn = Nothing releases the object reference held by the application.
Set conn = Nothing
For example:
conn.Close
Set conn = Nothing
The first statement closes the connection, while the second removes the reference to the object.
In classic Visual Basic and VBScript environments, explicitly releasing object references is a common practice for making resource management clear.
6. Checking Whether the Connection Is Open
Before closing a connection, an application can check its State property.
The Connection object's state can be examined using:
If conn.State = adStateOpen Then
conn.Close
End If
This prevents the application from unnecessarily attempting to close an already closed connection.
For example:
If Not conn Is Nothing Then
If conn.State = adStateOpen Then
conn.Close
End If
Set conn = Nothing
End If
This pattern is useful during cleanup because it checks both the existence of the object and the state of the connection.
7. Connection State
ADO provides the State property to determine whether a Connection object is currently open or closed.
A connection can have states such as:
adStateClosed
adStateOpen
For example:
If conn.State = adStateOpen Then
MsgBox "Connection is open"
Else
MsgBox "Connection is closed"
End If
This allows an application to make decisions based on the current connection state.
8. Closing a Connection When an Error Occurs
A particularly important aspect of cleanup is handling errors.
Suppose an application opens a connection and then encounters an error while executing a database operation. If the application does not properly clean up the connection, the connection may remain open longer than necessary.
A simplified Visual Basic example is:
On Error GoTo ErrorHandler
conn.Open
' Database operations
conn.Close
Set conn = Nothing
Exit Sub
ErrorHandler:
If Not conn Is Nothing Then
If conn.State = adStateOpen Then
conn.Close
End If
Set conn = Nothing
End If
The cleanup section ensures that the connection is closed even when an error occurs.
9. Closing a Connection and Recordsets
ADO applications frequently use both Connection and Recordset objects.
For example:
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
If a Recordset is using a connection, it is generally good practice to close the Recordset before closing the Connection.
If rs.State = adStateOpen Then
rs.Close
End If
Set rs = Nothing
If conn.State = adStateOpen Then
conn.Close
End If
Set conn = Nothing
The general cleanup sequence is therefore:
Finish database operations
|
Close Recordset
|
Release Recordset object
|
Close Connection
|
Release Connection object
This provides an organized way of releasing database-related resources.
10. What Happens When Close Is Called?
When:
conn.Close
is executed, ADO terminates the active connection to the data source.
After the connection has been closed, operations that require an open connection cannot be performed until the connection is opened again.
For example:
conn.Close
conn.Execute "SELECT * FROM Students"
would not be valid because the connection is no longer open.
The application would need to reopen the connection:
conn.Open
before performing operations that require an active connection.
11. Reusing a Connection Object
Closing a Connection object does not necessarily mean that the object cannot be reused.
For example:
conn.Open
' Perform operations
conn.Close
' Later
conn.Open
' Perform additional operations
The same Connection object can potentially be reopened when required, provided its configuration and underlying provider remain valid.
However, applications should avoid repeatedly opening and closing connections unnecessarily when an appropriate connection-management strategy, such as connection pooling, is being used.
12. Connection Pooling and Closing Connections
Connection pooling is a mechanism where database connections can be reused rather than establishing a completely new physical connection every time an application requests one.
When an application closes a connection, the underlying provider may return the connection to a connection pool rather than physically destroying the database connection immediately.
This means that:
conn.Close
is still important even when connection pooling is enabled.
It tells ADO and the underlying provider that the application has finished using the connection.
13. Best Practices for ADO Connection Cleanup
When working with ADO Connection objects, several practices are recommended.
First, open a connection only when it is needed.
Second, close the connection after completing the required database operations.
Third, close associated Recordset objects before releasing the Connection object when appropriate.
Fourth, release object references after completing their use.
For example:
If Not rs Is Nothing Then
If rs.State = adStateOpen Then
rs.Close
End If
Set rs = Nothing
End If
If Not conn Is Nothing Then
If conn.State = adStateOpen Then
conn.Close
End If
Set conn = Nothing
End If
Finally, cleanup should also be considered in error-handling code so that an unexpected error does not leave resources unnecessarily allocated.
14. Advantages of Proper Connection Cleanup
Proper ADO connection cleanup provides several benefits.
Better resource management: Database and application resources are released when they are no longer required.
Improved scalability: Applications serving many users can handle database connections more efficiently.
Reduced server load: Unnecessary open connections are avoided.
Better reliability: Proper cleanup reduces the possibility of resource-related problems.
Improved maintainability: Explicit cleanup makes the application's database lifecycle easier to understand and maintain.
15. Summary
The ADO Connection Close method is used to terminate an active database connection:
conn.Close
Closing the connection and releasing the object reference are separate operations:
conn.Close
Set conn = Nothing
For robust applications, the connection should be closed after database operations are completed, and cleanup should also be performed when an error occurs. When Recordsets are involved, they should generally be closed and released before the Connection object is cleaned up.
Therefore, proper ADO connection cleanup is an important part of database programming because it ensures that connections and related resources are not unnecessarily retained.