Platform SDK: Active Directory, ADSI, and Directory Services

Connection Caching

Once a connection has been made, the connection is cached on the client machine. If you reuse the same server, port, and credentials, ADSI will reuse the same handle. Handles are cached on all objects that have not been destroyed.

For performance benefits, you should keep object handles available and reuse them. If you lose the handle, you must re-bind, which causes an unnecessary trip across the network.

Dim cachedConn As IADs
Dim obj As IADs
Dim cachedName As String
Dim objName As String
 
' Connect to the server and keep this handle around to cache the connection.
Set cachedConn = GetObject("LDAP://MyMachine/DC=MyDomain,DC=Microsoft,DC=com")
 
cachedName = cachedConn.Get("distinguishedName")
Debug.Print (cachedName)
 
' Reuse the connection to MyMachine opened by cachedConn.
' Note that this line executes very fast because it doesn't
' have to go over the network again.
Set obj = GetObject("LDAP://MyMachine/CN=Bob,CN=Users,DC=MyDomain,DC=Microsoft,DC=com")
 
objName = obj.Get("distinguishedName")
Debug.Print (objName)
 
' Release the second connection.
Set obj = Nothing
 
' Reuse the connection to MyMachine opened by cachedConn again.
Set obj = GetObject("LDAP://MyMachine/CN=Administrator,CN=Users,DC=MyDomain,DC=Microsoft,DC=com")
 
objName = obj.Get("distinguishedName")
Debug.Print (objName)
 
' Release the second connection again.
Set obj = Nothing
 
' Release the first connection.
Set cachedConn = Nothing
 
' The connection to MyMachine is now closed.