I have a .pyt (Python Toolbox) in which I am creating a small tool that needs to edit features of Versioned Datasets but every time I try to start an edit session, for executing an update cursor to replace the geometries, I face the issue of Workspace already in transaction mode.
Now I have tried using the 'with_undo' and 'multiuser_mode' attributes of the startEditing() function but that does not seem to work. Another popular solution I found online was commenting the startOperation() and stopOperation() which failed too.
I am adding a snippet of my code for reference. Is there any other solution to this problem?
Please note that in the following snippet, the data provided as input is versioned.
import arcpy
def startediting(self, outpath):
# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(outpath)
# Edit session is started without an undo/redo stack for versioned data
# (for second argument, use False for unversioned data)
edit.startEditing()
# Start an edit operation
edit.startOperation()
return edit
def stopediting(self, edit):
# Stop the edit operation.
edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)
def snap(self, fclayer, nearfeat, primaryfeat, outpath, tolerance, in_gdb):
""" fclayer - the feature class layer containg the feature that has to be snapped
nearfeat- ref_ID of the feature it has to be snapped to
primaryfeat- ref_ID of the feature that has to be snapped
outpath- Output Workspace
tolerance- the tolerance of the feature to be snapped to (Minimum distance)
in_gdb- Input Workspace
Please note: a copy of the feature is made so because the features that have to be snapped are within the same feature class.
Which is why updateCursor is later used to replace the geometry of the original feature after editing."""
tempfeat=outpath+"\\"+"tempnearfeat"
arcpy.SelectLayerByAttribute_management(in_layer_or_view=fclayer, selection_type="NEW_SELECTION", where_clause="ref_ID = '"+nearfeat+"'")
arcpy.CopyFeatures_management(fclayer, tempfeat)
#Overwriting the near feature
refshape=""
arcpy.Snap_edit(fclayer, [[tempfeat, "END", tolerance]])
with arcpy.da.SearchCursor(tempfeat, ["ref_ID", "shape@"], "ref_ID = '"+nearfeat+"'") as cursor:
for row in cursor:
refshape=row[1]
edit= startediting(in_gdb)
with arcpy.da.UpdateCursor(fclayer, ["ref_id", "shape@"], "ref_ID = '"+primaryfeat+"'") as cursor:
#edit.startOperation()
for row in cursor:
row[1]=refshape
cursor.updateRow(row)
arcpy.Delete_management(tempfeat)
stopediting(edit)
#edit session closed