6

In a Python script that I use to publish ArcGIS 10.0 Geoprocessing Services I have some code:

arcpy.AddMessage("If process hangs here, check for ArcSDE connections issue by trying to open " + mapFile + " in ArcMap")
mxd = arcpy.mapping.MapDocument(mapFile)

The reason I have it is that from time to time the maximum number of SDE Connections that the server can support may have been exceeded.

Before I hit this I would like to have ArcPy test:

  1. If the SDE Connection can be reached at all; and
  2. If the SDE Connection is to an ArcSDE which is responding within a reasonable time.

Does ArcPy have any means of testing SDE connections before trying to use them?

3 Answers 3

4

Yes, you can do something like:

try:     
      if arcpy.Exists(PropertyTax_Parcels):
         arcpy.AddMessage("Checking core Layer found? Yes and online")
except:
      raise Exception("Layer not found")

You just define it as a static layer in your SDE and if so it can continue; other wise you else out of the script. I do this in many places and will actually log and fire SMTP messages if my connections are lost in core apps.

12
  • Thanks @D.E.Wright - expect to see me accept your answer as soon as I have had a chance to test "breaking" the SDE connection and seeing how well it handles that condition using your code snippet.
    – PolyGeo
    Sep 16, 2011 at 6:44
  • Key is just having the layer there that you want to check; you can't just check for a connection; but being able to connect to the layer gives you a status. Very crude; but that is all we really have in the limited scope.
    – D.E.Wright
    Sep 16, 2011 at 19:56
  • 1
    What if you do not know what is in the GDB? I use arcpy.ListDatasets(), which returns an empty list if the connection is bad. But this is not a sure fire solution.
    – Barbarossa
    Feb 12, 2019 at 21:29
  • That is a good potential check though.
    – D.E.Wright
    Feb 13, 2019 at 17:02
  • This fails for a bad user login on an SDE (as in it will return True anyway). I've found arcpy.Exists to be unreliable to actually determine existance of GIS data sometimes. Things like empty folders with a .gdb extension will return True as well. I've had to resort to using arcpy.Describe to verify and check the dataType afterwards to verify a valid workspace or mxd.
    – vman
    Apr 9, 2019 at 11:31
2

Use this to check a valid SDE connection.

import arcpy
    
sde = r"C:/Path/To/SDE.sde"
        
exists = arcpy.Describe(sde).dataType == "Workspace"
if not exists:
   print("Not a workspace ({})".format(sde))

Only use arcpy.Exists() once you've verified the SDE works, as it is unreliable unless an SDE can be connected to.

2
  • @Steve if you've tried it, you'll know that a Describe object is lazily loaded. When you try and access the properties you'll find it'll take up additional time as it loads. I've tried your method vs using Describe and there isn't a major difference in time taken to make the connection from what I've seen. For the sake of simplicity and fewer dependencies then this is fine. The code is only tested in Python 2.7 which is available in ArcGIS Desktop 10.8, and if you note the OP is using arcpy.mapping so the code works for them.
    – vman
    Feb 11 at 18:39
  • 1
    good to know. I was printing the describe object which was accessing every property and being super slow. Thanks for the lazy reminder!
    – Steve
    Feb 14 at 16:04
2

Another way

import arcpy
import sys

from pathlib import Path
from tqdm import tqdm

input = sys.argv[1]
connection_strings = Path(input)

exists = connection_strings.exists()

print(f'{connection_strings.name} found: {exists}')

if not exists:
    sys.exit(0)

sde_files = list(connection_strings.rglob('*.sde'))

print(f'found {len(sde_files)} databases to check')

failures = []

items = tqdm(sde_files)
for connection in items:
    items.set_postfix_str(connection.name)
    
    try:
        arcpy.ArcSDESQLExecute(str(connection))
    except:
        failures.append(f'{connection.name} is not accessible')

[print(item) for item in failures]

You can place the above python in a file called test.py and run it from a terminal to validate all the .sde files within a provided folder. Be sure to install your dependencies first.

conda create --name sde-tester
activate sde-tester
conda install -c esri arcpy
pip install tqdm

python test.py "c:\projects"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.