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?


Yes, you can do something like:

      if arcpy.Exists(PropertyTax_Parcels):
         arcpy.AddMessage("Checking core Layer found? Yes and online")
      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.

| improve this answer | |
  • 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 '11 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 '11 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 '19 at 21:29
  • That is a good potential check though. – D.E.Wright Feb 13 '19 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 '19 at 11:31

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.

| improve this answer | |

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.