How do I disconnect an open SDE direct connection?

6625
8
06-18-2014 01:07 PM
DavidWatkins2
New Contributor II
I am working in an enterprise geodatabase in through Citrix - both the desktop client and SDE (Oracle 11g) are at 10.1.

I often have multiple SDE connections open in ArcCatalog and would like a way to close all of them at once rather than right clicking on each one and selecting disconnect. I don't see any close or disconnect methods and the 'ClearWorkspaceCache_management' method does not close the connection in the ArcCatalog tree.

Does anyone know if there is a way to do this using ArcObjects?

Thank you
0 Kudos
8 Replies
AhmedEl-Sisi
Occasional Contributor III
Hello David,

I have implemented a method which will loop over a folder contents (selected folder or specific folder name) and close all active database connections inside it.
    private void DisconnectActiveSDEConnections(IGxApplication gxApp, bool useSelectedFolder = false, string folderName = "Database Connections")
        {
            IGxCatalog gxCatalog = gxApp.Catalog;
            object connectionsFolder = null;
            IGxSelection gxSel = null;
            IGxObject gxObj = null;
            int numFound;

            try
            {
                if (useSelectedFolder)
                {
                    gxSel = gxApp.Selection;
                    gxObj = gxSel.Location;
                }
                else
                {
                    connectionsFolder = gxCatalog.GetObjectFromFullName(folderName, out numFound);
                    if (numFound != 1 || connectionsFolder == null)
                    {
                        MessageBox.Show("Folder not found!");
                        return;
                    }
                    gxObj = connectionsFolder as IGxObject;
                }

                if (gxObj is IGxObjectContainer)
                {
                    IGxObjectContainer gxObjCont = null;
                    IEnumGxObject gxEnum = null;
                    IGxObject gxObject = null;
                    IGxDatabase2 gxDatabase = null;
                    gxObjCont = gxObj as IGxObjectContainer;
                    gxEnum = gxObjCont.Children; //enumerate folder contents
                    gxObject = gxEnum.Next();

                    while (gxObject != null)
                    {
                        gxDatabase = gxObject as IGxDatabase2;

                        Debug.Print(gxObject.Category);

                        if (gxDatabase != null && gxDatabase.IsConnected)
                        {
                            gxDatabase.Disconnect();
                              Debug.Print(gxObject.FullName + " Disconnected");
                        }
                        gxObject = gxEnum.Next();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

Regards,
Ahmed
DavidWatkins2
New Contributor II


Ahmed,

I tried that solution and it worked perfectly. It did exactly what I wanted. Thank you for sharing with me.

Dave

0 Kudos
TracieStreltzer
New Contributor III

Anyone figured out a way to do this in python instead?

0 Kudos
TomGeo
by
Occasional Contributor III

What if you try it in Python in a 'with' environment? Files and alike are usually closed when leaving the environment. Close is basically invoked.

Don't know if it works with an SDE connection the same way...

- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
0 Kudos
AsrujitSengupta
Regular Contributor III
0 Kudos
TracieStreltzer
New Contributor III

Thanks - yes, I had found this solution for the actual disconnect,, but I have 30 databases.  I would like to be able to cycle through a list of connection files in a folder and disconnect all users in each connection file prior to upgrading and restarting servers, etc.  I had an ArcSDE command line to do this on Unix, but since that is going away I'd like to replace it with something I can mange with Python that can do both SQL Server and Oracle instances.. Otherwise I may have to use Poewershell to manage this portion of the script...just wondering if anyone else had worked this out yet...

0 Kudos
AhmedEl-Sisi
Occasional Contributor III

By following the steps in this post I can run my ArcObjects code inside a python script from Arcmap,

I just rewrite part of the disconnect snippet as a POC and tested it from Arcmap python window and from toolbox and it woks fine.

def ArcCatalog_DisconnectSDEConnections():
    GetDesktopModules()

    pApp = GetCurrentApp()
    import comtypes.gen.esriFramework as esriFramework
    import comtypes.gen.esriCatalogUI as esriCatalogUI
    import comtypes.gen.esriCatalog as esriCatalog
    pGxApp = CType(pApp, esriCatalogUI.IGxApplication)
    print pGxApp
    pGxObj = pGxApp.SelectedObject
    if not pGxObj:
        print "Nothing selected."
        return
    pGxContainer = CType(pGxObj, esriCatalog.IGxObjectContainer)
    if not pGxContainer:
        print "No folder selected."
        return

    pgxEnum =CType(pGxContainer.Children,esriCatalog.IEnumGxObject)

    gxObject =CType(pgxEnum.Next(),esriCatalog.IGxObject)

    while gxObject:

        gxDatabase = CType(gxObject,esriCatalog.IGxDatabase)
        #print gxDatabase.IsConnected
        if gxDatabase and gxDatabase.IsConnected:
            gxDatabase.Disconnect()
            print gxObject.FullName + " Disconnected"

        gxObject =CType(pgxEnum.Next(),esriCatalog.IGxObject)

I'm using Arcmap 10.2.1, I added the code to the 10.2 snippets file and called it directly after importing the file.

import arcpy
import sys
sys.path.append(r'FOLDER_CONTAINS_SNIPPET_FILE')
from Snippets import ArcCatalog_DisconnectSDEConnections
try:

    ArcCatalog_DisconnectSDEConnections()

except:
    arcpy.AddMessage(sys.exc_info())

You should check "Run Python script in process" checkbox in script properties to get it works.

Hope this approach helps.

0 Kudos
TracieStreltzer
New Contributor III

That’s great, thank you!!!

Tracie

Tracie Streltzer

GIS Application Administrator - Lead

South Florida Water Management District

561.682.6134 | tstrelt@sfwmd.gov<mailto:tstrelt@sfwmd.gov>

0 Kudos