Select to view content in your preferred language

Arcpy: cannot get exclusive schema lock even if I use arcpy.DisconnectUser method

2847
4
06-12-2018 06:14 AM
by Anonymous User
Not applicable

Hi, I'm trying to delete some coded values from a domain in arcpy, but my script fails due to a schema lock. I'm on an arcsde enterprise geodatabase in oracle.
To force user disconnection, i use the following method:

def getExclusiveLock(admin_workspace):
   if admin_workspace == '':
      return

   hostName = socket.gethostname()

   # Look through the users in the connected user list and get the SDE ID.
   # Use the SDE ID to disconnect the user that matches the username variable
   users = arcpy.ListUsers(admin_workspace)
   for item in users:
      if hostName.lower() not in item.ClientName.lower():
         arcpy.DisconnectUser(admin_workspace, item.ID)

   arcpy.AcceptConnections(admin_workspace, False)

This is the code I use to delete the coded values:

workspaces = [r'myWorkspace1', r'myWorkspace2' ]

sde = [r'sdeWorkspace1', r'sdeWorkspace2']

sdeIndex = 0

for ws in workspaces:

   domainValues= {}

   for domain in arcpy.da.ListDomains(ws):
      if domain.name == 'myDomain':
         domainValues = domain.codedValues
         break

   getExclusiveLock(sde[sdeIndex])

   f = open(r'fileWithCodedValuesToDelete', 'rb')
   reader = csv.reader(f, delimiter = ';')
   for row in reader:

      try:

         arcpy.DeleteCodedValueFromDomain_management(ws, 'myDomain', int(row[0]))
         arcpy.AddCodedValueToDomain_management(ws, 'myDomain', int(row[0]), row[0] + ' - ' + row[1])
      except KeyError:
         print "Key {0} not found".format(row[0])
         arcpy.AddCodedValueToDomain_management(ws, 'myDomain', int(row[0]), row[0] + ' - ' + row[1])
      except UnicodeDecodeError:
         print "Decode error {0}".format(row[1])

   releaseExclusiveLock(sde[sdeIndex])
   sdeIndex = sdeIndex + 1

The script always returns the error "cannot get exclusive schema lock", but then, opening arcatalog, I see that there are no users connected, but the sde user.

Does anyone know why I cannot acquire the lock?

Tags (1)
0 Kudos
4 Replies
JoeBorgione
MVP Emeritus

Vincenzo-  I've copied and pasted your code into a 'syntax highlighter' window to make it easier for potential responders to read. ( In the main menu, click More, then choose Syntax Highlighter)

I don't see where your workspaces are truly defined.  Typically, one would expect to see:

ws_1 = r'C:\some\path\to\a\database'
ws_2 = r'D:\some\other\path\to\database'

and then refer to the appropriate variables.  Perhaps you simply did not copy those lines?

workspaces = [r'myWorkspace1', r'myWorkspace2' ]

sde = [r'sdeWorkspace1', r'sdeWorkspace2']

sdeIndex = 0

for ws in workspaces:

   domainValues= {}

   for domain in arcpy.da.ListDomains(ws):
      if domain.name == 'myDomain':
         domainValues = domain.codedValues
         break

   getExclusiveLock(sde[sdeIndex])

 

   f = open(r'fileWithCodedValuesToDelete', 'rb')
   reader = csv.reader(f, delimiter = ';')
   for row in reader:

      try:

         arcpy.DeleteCodedValueFromDomain_management(ws, 'myDomain', int(row[0]))
         arcpy.AddCodedValueToDomain_management(ws, 'myDomain', int(row[0]), row[0] + ' - ' + row[1])
      except KeyError:
         print "Key {0} not found".format(row[0])
         arcpy.AddCodedValueToDomain_management(ws, 'myDomain', int(row[0]), row[0] + ' - ' + row[1])
      except UnicodeDecodeError:
         print "Decode error {0}".format(row[1])

   releaseExclusiveLock(sde[sdeIndex])
   sdeIndex = sdeIndex + 1
That should just about do it....
0 Kudos
by Anonymous User
Not applicable

Thank you, the workspaces are iterated through the for each loop and are defined by the two lists:

workspaces = [r'myWorkspace1', r'myWorkspace2' ]

sde = [r'sdeWorkspace1', r'sdeWorkspace2']

Each entry in the "workspaces" list has the corresponding administrator workspace in the "sde" list.

0 Kudos
MattFrancis1
Emerging Contributor

Did you get this solved?  Also, would be nice to see the releaseExclusiveLock(sde[sdeIndex]) function.

0 Kudos
ConnorFriese
Occasional Contributor

vincenzo deriu

Have you looked at the "Locks" tab in the Geodatabase Administration window?

To get here:

1. Right-click on the sde connection file (using Geodatabase Administrator credentials)

2. Hover over Administration

3. Click Administer Geodatabase...

4. Click Locks tab

Are there any locks listed here when this error is being received?

http://desktop.arcgis.com/en/arcmap/10.3/manage-data/gdbs-in-postgresql/manage-geodatabase-locks.htm

0 Kudos