Revitalizing this thread: because ArcGIS Pro 3.6 and Python 3.13
UPDATE: i figured it out. The edit object has to be created inside the try statement. code block corrected below. Also make sure you run as a geoprocess in the foreground, or you won't see the results.
I was troubleshooting re-appearance in Pro 3.6 of 'workspace already in transaction mode' I worked around successfully in Pro 3.5.x
I'm trying to update a date field to 'now' for all selected features in the active map, which I've set up to trigger each feature class's attribute rules. I finally got the 'workspace' changes dialed-in from the Pro 3.6 env, but I'm running into the old 'workspace already in transaction mode' issue.
This is being run as a Toolbox Script.
I tried 'edit.startEditing(False,False)' and 'edit.startEditing(False,True)' and get the same error.
import arcpy
from datetime import datetime
import os
import sys
from time import sleep
now = datetime.now()
username = os.environ.get('USERNAME')
nversion = "dbo." + username
lyrsource = r"C:\Gisdata\Projects\ArcGIS_Pro\Projects\TraditionalEditingTesting\SQLServer-androtest-MWWSSB(TestDB).sde\mwwssb.dbo.wserviceconnection"
androtemp_sde = arcpy.Describe(lyrsource).workspace
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.activeMap
newline = '\n'
def execute():
if m:
feature_layers = m.listLayers()
has_selection = False
selected_layers = []
for lyr in feature_layers:
if lyr.isFeatureLayer:
selection_set = lyr.getSelectionSet()
if selection_set:
selected_layers.append(lyr.name)
has_selection = True
if not has_selection:
arcpy.AddError("No selected features detected.")
sys.exit(0)
else:
if has_selection:
arcpy.AddMessage("Selections found in:" + newline + f"{newline.join(selected_layers)}")
arcpy.AddMessage(newline)
for lyr in m.listLayers():
if lyr.isFeatureLayer and lyr.getSelectionSet():
try:
edit = arcpy.da.Editor(androtemp_sde, multiuser_mode=True, version=nversion)
edit.startEditing()
edit.startOperation()
# with edit:
cursor = arcpy.da.UpdateCursor(lyr, 'DART')
for row in cursor:
row[0] = now
cursor.updateRow(row)
sleep(0.5)
del cursor
del row
arcpy.AddMessage(f"{lyr.name} Updated.")
edit.stopOperation()
edit.stopEditing(True)
except Exception as e:
arcpy.AddError(f"Error updating {lyr.name}: {e}")
if edit.isEditing:
edit.stopOperation()
arcpy.AddError("operation stopped in exception")
edit.stopEditing(False) ## Stop the edit session with False to abandon the changes
arcpy.AddError("edit stopped in exception")
finally:
# Cleanup
arcpy.ClearWorkspaceCache_management()
return
else:
arcpy.AddError("No active map.")
sys.exit(0)
execute()