Sunday, 5 January 2020

arcobjects - Remove lock on feature class


When i try to delete a feature class , after or during the use of an Arcgis engine app which uses the same feature class i got an error concerning the lock , so i can't get ride of this lock until i restart the computer.


i would like to know a way to remove locks using arcpy or arcobjects 10.1 , it would be perfect if i can get ride of the lock inside the arcengine app which also locks the geodatabase , i have seen different posts in SE about locks but no one of them gives the solution to free locks using ArcObjects.



Answer




If you are using Arcpy scripting, data lock issues have become even more problematic with 10.1.



If the locks are being created by your code, then you aren't cleaning up after yourself. Release all references to feature classes, workspaces, cursors, etc. The locks are removed when you have no more references to the objects being locked.




Well, that is the way it should work, but it rarely does. Usually a code will work occasionally, but often crash inexplicably in different places.


The problem appears to be that locks are just left sitting there `for a while', but the code runs much faster than the speed at which the locks are removed. However, some of the inbuilt Arc tools seem to force locks to be cleared on demand (as locks apply to entire GDBs all at once, a lock will prevent you from working with any of the contained Feature Classes). These tools are arcpy.Compact_management() and arcpy.Exists().


Here is a little function that I use within my code that has dramatically increased reliability (for a script that creates and edits multiple GDBs and Feature Classes within them):


def clearWSLocks(inputWS):
'''Attempts to clear locks on a workspace, returns stupid message.'''
if all([arcpy.Exists(inputWS), arcpy.Compact_management(inputWS), arcpy.Exists(inputWS)]):
return 'Workspace (%s) clear to continue...' % inputWS
else:
return '!!!!!!!! ERROR WITH WORKSPACE %s !!!!!!!!' % inputWS


It is used by simply passing the workspace (GDB) path to the function, and should be done after every operation on either the workspace (i.e. GDB creation) or Feature Classes within the workspace (i.e. Cursors, adding fields, calculations, etc.). For example (shown here as a standalone script, with the function at the top; to use the function, copy it and paste it between the imports and the actual program, as shown here):


import arcpy

def clearWSLocks(inputWS):
'''Attempts to clear locks on a workspace, returns stupid message.'''
if all([arcpy.Exists(inputWS), arcpy.Compact_management(inputWS), arcpy.Exists(inputWS)]):
return 'Workspace (%s) clear to continue...' % inputWS
else:
return '!!!!!!!! ERROR WITH WORKSPACE %s !!!!!!!!' % inputWS


GDBpath = 'C:/Temp/'
GDBname = 'Test.gdb'
tableName = 'SweetFC'
arcpy.CreateFileGDB_management(GDBpath, GDBname)
print(clearWSLocks(GDBpath+GDBname))
arcpy.CreateTable_management(GDBpath+GDBname, tableName)
print(clearWSLocks(GDBpath+GDBname))
# etc....

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...