Select to view content in your preferred language

Arcpy editor transaction rollback

1000
3
02-13-2020 05:13 PM
VictorTey
Esri Contributor

Hi,

I have a feature class without version and archiving.  When I attempt the code below, it threw a workspace already in transaction mode error. I tested with all combination of 

  • edit.startEditing(True/False,True/False)
  • With and without startOperation

nothing works. the only thing however that did worked is using arcpy.InsertCursor instead of arcpy.da.InsertCursor

def main():
    try:
        edit = arcpy.da.Editor(workspace)
        edit.startEditing(False, False)
        edit.startOperation()
        point = arcpy.Point(111,-60)
        pointGeom = arcpy.PointGeometry(point,"4326")

        row = []
        row.append('test')
        row.append(pointGeom)

        cursor = arcpy.da.InsertCursor(config.FC, ['testfield', 'SHAPE@XY'])
        cursor.insertRow(row)

        edit.stopOperation()
        edit.stopEditing(True)
    except Exception as e:
        #error handling


The following also did not work

I would like to roll back if an error occurred during a truncate and append. I tested with DeleteFeature, neither worked.

 with arcpy.da.Editor(workspace) as edit:
     arcpy.TruncateTable_management(config.PropertyFC)
    #arcpy.DeleteFeatures_management(config.PropertyFC)
     arcpy.Append_management(config.PropertyGDB, config.PropertyFC,"TEST")

Can someone advise?

Thanks

0 Kudos
3 Replies
by Anonymous User
Not applicable

Hi Victor,

Ok...arcpy has a tendency to hold onto cursor objects.  But it does treat arcpy.da.InsertCursor as different to arcpy.InsertCursor, so it looks like it is holding onto a da cursor either from earlier in the script or from an earlier iteration.

Things to check:

  • Version of Python you are using
  • Version of ArcPy
  • that there are no other open edit connections to the workspace

Several things to try:

  • close down your IDE and Arc session then reopen
    • some IDEs do not fully release objects and Arc is really clingy
  • in the script make sure you delete the any cursor and row objects after you have finished with them
    • this will make sure that other cursor objects will not interfere
  • Move the set up outside of the Try statement
    • this is mainly to improve performance when iterating over the table
def main():

    workspace = r'???'

    edit = arcpy.da.Editor(workspace)
    
    point = arcpy.Point(111,-60)
    pointGeom = arcpy.PointGeometry(point,"4326")

    try:
        edit.startEditing(False, False)
        edit.startOperation()        
        cursor = arcpy.da.InsertCursor(config.FC, ['testfield', 'SHAPE@XY'])
        row = ['test', pointGeom]
        cursor.insertRow(row)
        edit.stopOperation()
        edit.stopEditing(True)

        del cursor, row

    except Exception as e:
        #error handling‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

A another way would be:

def main():

    workspace = r'???'

    edit = arcpy.da.Editor(workspace)
    
    point = arcpy.Point(111,-60)
    pointGeom = arcpy.PointGeometry(point,"4326")

    try:
        edit.startEditing(False, False)
        edit.startOperation()        
        cursor = arcpy.da.InsertCursor(config.FC, ['testfield', 'SHAPE@XY'])
        row = cursor.newRow()
        row.SetValue('testfield', 'test')
        row.SetValue('SHAPE@XY', pointGeom)
        edit.stopOperation()
        edit.stopEditing(True)

        del cursor, row

    except Exception as e:
        #error handling‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Onto this one:

The following also did not work

I would like to roll back if an error occurred during a truncate and append. I tested with DeleteFeature, neither worked.

 with arcpy.da.Editor(workspace) as edit:
     arcpy.TruncateTable_management(config.PropertyFC)
    #arcpy.DeleteFeatures_management(config.PropertyFC)
     arcpy.Append_management(config.PropertyGDB, config.PropertyFC,"TEST")

Couple of questions:

  • is this in a "Try" statement?
    • a "try" should roll back if there is an error
  • are you trying to backup from a GDB to a Shapefile?
  • Shapefiles will truncate long field names 

Well, I hope that this helps.

Michael

VictorTey
Esri Contributor

Thank you for that, however I have tried as you have suggested but nothing worked. The example I was reduce to be as simple as possible from the original code therefore nothing was running before it. 

The data is retrieve from a rest api and pushed into a FC, no gdb or shapefile involved.

Unfortunately due to time constraint, i had to work around this.

Again thank you for your time

by Anonymous User
Not applicable

No problems Victor, happy to help.

Sorry we couldn't find the solution.