I've run up against a wall trying to get an edit session to work on a version. These are the scenarios Ive gone through in the code and their error mesages:
1 No edit session: 'Objects in this class cannot be updated outside an edit session'
2 Setting up an edit session not as a context manager: 'workspace already in transaction mode'
3 Setting up an edit session as a context manager: 'The requested operation is invalid on a closed state'
So I have to start an edit session but when I start one it says I am already in transaction mode. What?
Can you post some of the code that you are trying to execute? There is an example in the help for how to work with this:
import arcpy
import os
fc = 'Database Connections/Portland.sde/portland.jgp.schools'
workspace = os.path.dirname(fc)
# Start an edit session. Must provide the worksapce.
edit = arcpy.da.Editor(workspace)
# Edit session is started without an undo/redo stack for versioned data
# (for second argument, use False for unversioned data)
edit.startEditing(False, True)
# Start an edit operation
edit.startOperation()
# Insert a row into the table.
with arcpy.da.InsertCursor(fc, ('SHAPE@', 'Name')) as icur:
icur.insertRow([(7642471.100, 686465.725), 'New School'])
# Stop the edit operation.
edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)
Not as a context manager:
target = r'D:\GIS-DATA\bay-arcgis-pro_ParcelEditing.sde\sde.SDE.Staging\sde.SDE.SPRCodeEnforcementCasesStaging'
try:
edit = arcpy.da.Editor(r'D:\GIS-DATA\bay-arcgis-pro_ParcelEditing.sde')
edit.startEditing(False,False)
edit.startOperation()
with arcpy.da.UpdateCursor(target,'*') as cursor:
for row in cursor:
row[3] = 'test'
cursor.updateRow(row)
finally:
edit.stopOperation()
edit.stopEditing(True)
yeilds:
Traceback (most recent call last):
File "D:\GIS-DATA\editortest.py", line 9, in <module>
for row in cursor:
RuntimeError: workspace already in transaction mode
As a context manager:
target = r'D:\GIS-DATA\bay-arcgis-pro_ParcelEditing.sde\sde.SDE.Staging\sde.SDE.SPRCodeEnforcementCasesStaging'
with arcpy.da.Editor(r'D:\GIS-DATA\bay-arcgis-pro_ParcelEditing.sde'):
with arcpy.da.UpdateCursor(target,'*') as cursor:
for row in cursor:
row[3] = 'test'
cursor.updateRow(row)
yeilds:
Traceback (most recent call last):
File "D:\GIS-DATA\editortest.py", line 8, in <module>
cursor.updateRow(row)
RuntimeError: The requested operation is invalid on a closed state
As a side note, I have the startEditing with False,False here but have tried every permutation of Boolean, Boolean you can and always get the same result.
Did you ever get this figured out?
As much as I encourage people to not reinvent the wheel, or re-ask questions that have already been asked, I think it would be better in this case to start a new question and provide some specifics of what isn't working. Not only is the thread a couple years old, neither the OP nor commenter are very active on GeoNet.
I completely disagree with you Josua - if one searches forums (any forum) for esri related issues one constantly finds unanswered questions. It would be useful to everyone working with esri products if it were easier to find solutions to common problems instead of get lost searching through thread after unanswered duplicate thread.
The only way I could get it to work in my versioned SQL Server SDE is with_undo=False, multiuser_mode=True:
edit.startEditing(False, True)
To each his own. The startEditing() method has two Boolean paramaters, 4 combinations in all. You have given us what combination works, but what combination are you interested in? And, what is the error message you are getting?