How to manage connections with the Connection Manager


This sample demonstrates how to use the Connection Manager to manage connections to ArcGIS Server and ArcIMS. Only ArcGIS Server Local connections and ArcIMS HTTP and TCP connections are supported.
  1. Set the username, password, and domain variables to a user account in the agsuser or agsadmin groups on the SOM. Define the servername variable to be the machine on which the SOM is running.
  2. Set the serverobjectname to the name of an ArcGIS Server map service.
[C#]
ESRI.ArcGIS.ADF.Connection.ConnectionManager m_ConnectionManager = new
    ESRI.ArcGIS.ADF.Connection.ConnectionManager(null);

// ArcGIS Server Local
ESRI.ArcGIS.ADF.Identity agsIdentity = new ESRI.ArcGIS.ADF.Identity("test", 
    "secret.password", "mydomain");

ESRI.ArcGIS.ADF.Connection.Connection primaryConnectionArcGIS = new
    ESRI.ArcGIS.ADF.Connection.Connection("server1",
    ESRI.ArcGIS.ADF.Connection.ConnectionMode.RoundRobin, agsIdentity, 3, 3);
primaryConnectionArcGIS.ServerType = "ArcGIS";

// Add alternative connections
primaryConnectionArcGIS.Items.Add(new ESRI.ArcGIS.ADF.Connection.ConnectionItem(
    "server2"));
primaryConnectionArcGIS.Items.Add(new ESRI.ArcGIS.ADF.Connection.ConnectionItem(
    "server3"));
primaryConnectionArcGIS.Items.Add(new ESRI.ArcGIS.ADF.Connection.ConnectionItem(
    "server4"));
m_ConnectionManager.Add(primaryConnectionArcGIS);

// ArcIMS TCP
ESRI.ArcGIS.ADF.Connection.Connection primaryConnectionArcIMSTCP = new
    ESRI.ArcGIS.ADF.Connection.Connection("server2@5300",
    ESRI.ArcGIS.ADF.Connection.ConnectionMode.Failover, null, 3, 3);
primaryConnectionArcIMSTCP.ServerType = "ArcIMS";

// Add alternative connections
primaryConnectionArcIMSTCP.Items.Add(new ESRI.ArcGIS.ADF.Connection.ConnectionItem(
    "server3@5300"));
m_ConnectionManager.Add(primaryConnectionArcIMSTCP);

// ArcIMS HTTP
ESRI.ArcGIS.ADF.Identity imsIdentity = new ESRI.ArcGIS.ADF.Identity("test", 
    "pass.word", "");

ESRI.ArcGIS.ADF.Connection.Connection primaryConnectionArcIMSHTTP = new
    ESRI.ArcGIS.ADF.Connection.Connection(
    "http://server2:8080/servlet/com.esri.esrimap.Esrimap", imsIdentity);
primaryConnectionArcIMSHTTP.ConnectionMode =
    ESRI.ArcGIS.ADF.Connection.ConnectionMode.Default;
primaryConnectionArcIMSHTTP.ServerType = "ArcIMS";

// Add alternative connections
primaryConnectionArcIMSHTTP.Items.Add(new ESRI.ArcGIS.ADF.Connection.ConnectionItem(
    "http://server3:8080/servlet/com.esri."));
m_ConnectionManager.Add(primaryConnectionArcIMSHTTP);

// Use connection manager to get a server connection.  Specify the name of the primary
// connection item and the server type: "ArcGIS" and "ArcIMS".
// If round-robin, the primary and alternate connection items are cycled through in order
// If failover, the primary connection item is returned.  If unavailable, the alternative 
// connection items are used in order.
// If default, only the primary connection item is used.
ESRI.ArcGIS.ADF.Connection.IServerConnection serverConnection =
    m_ConnectionManager.ServerConnection("server1", "ArcGIS");

// Connection should be returned alive (connected)
bool isAlive = serverConnection.IsAlive();

// Cast to the appropriate connection type:
// ArcGIS - ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection
// ArcIMS - ESRI.ArcGIS.ADF.Connection.IMS.IMSServerConnection
if (serverConnection is ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection)
{
    ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsConnection = 
        (ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection)serverConnection;
}
[VB.NET]
Dim m_ConnectionManager As ESRI.ArcGIS.ADF.Connection.ConnectionManager = New ESRI.ArcGIS.ADF.Connection.ConnectionManager(Nothing)

' ArcGIS Server Local
Dim agsIdentity As ESRI.ArcGIS.ADF.Identity = New ESRI.ArcGIS.ADF.Identity("test", "secret.password", "mydomain")

Dim primaryConnectionArcGIS As ESRI.ArcGIS.ADF.Connection.Connection = New ESRI.ArcGIS.ADF.Connection.Connection("server1", ESRI.ArcGIS.ADF.Connection.ConnectionMode.RoundRobin, agsIdentity, 3, 3)
primaryConnectionArcGIS.ServerType = "ArcGIS"

' Add alternative connections
primaryConnectionArcGIS.Items.Add(New ESRI.ArcGIS.ADF.Connection.ConnectionItem("server2"))
primaryConnectionArcGIS.Items.Add(New ESRI.ArcGIS.ADF.Connection.ConnectionItem("server3"))
primaryConnectionArcGIS.Items.Add(New ESRI.ArcGIS.ADF.Connection.ConnectionItem("server4"))
m_ConnectionManager.Add(primaryConnectionArcGIS)

' ArcIMS TCP
Dim primaryConnectionArcIMSTCP As ESRI.ArcGIS.ADF.Connection.Connection = New ESRI.ArcGIS.ADF.Connection.Connection("server2@5300", ESRI.ArcGIS.ADF.Connection.ConnectionMode.Failover, Nothing, 3, 3)
primaryConnectionArcIMSTCP.ServerType = "ArcIMS"

' Add alternative connections
primaryConnectionArcIMSTCP.Items.Add(New ESRI.ArcGIS.ADF.Connection.ConnectionItem("server3@5300"))
m_ConnectionManager.Add(primaryConnectionArcIMSTCP)

' ArcIMS HTTP
Dim imsIdentity As ESRI.ArcGIS.ADF.Identity = New ESRI.ArcGIS.ADF.Identity("test", "pass.word", "")
Dim primaryConnectionArcIMSHTTP As ESRI.ArcGIS.ADF.Connection.Connection = New ESRI.ArcGIS.ADF.Connection.Connection("http://server2:8080/servlet/com.esri.esrimap.Esrimap", imsIdentity)
primaryConnectionArcIMSHTTP.ConnectionMode = ESRI.ArcGIS.ADF.Connection.ConnectionMode.Default
primaryConnectionArcIMSHTTP.ServerType = "ArcIMS"

' Add alternative connections
primaryConnectionArcIMSHTTP.Items.Add(New ESRI.ArcGIS.ADF.Connection.ConnectionItem("http://server3:8080/servlet/com.esri."))
m_ConnectionManager.Add(primaryConnectionArcIMSHTTP)

' Use connection manager to get a server connection.  Specify the name of the primary
' connection item and the server type: "ArcGIS" and "ArcIMS".
' If round-robin, the primary and alternate connection items are cycled through in order
' If failover, the primary connection item is returned.  If unavailable, the alternative
' connection items are used in order.
' If default, only the primary connection item is used.
Dim serverConnection As ESRI.ArcGIS.ADF.Connection.IServerConnection = m_ConnectionManager.ServerConnection("server1", "ArcGIS")

' Connection should be returned alive (connected)
Dim isAlive As Boolean = serverConnection.IsAlive()

' Cast to the appropriate connection type:
' ArcGIS - ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection
' ArcIMS - ESRI.ArcGIS.ADF.Connection.IMS.IMSServerConnection
If TypeOf serverConnection Is ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection Then
    Dim agsConnection As ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection = CType(serverConnection, ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection)
End If