Python arcpy.da.InsertCursorworkspace: already in transaction mode

2039
2
03-04-2016 10:34 AM
JoseSanchez
Occasional Contributor III

Hello everyone,

I am exporting code from arcpy.InsertCursor to arcpy.da.InsertCursor and I am getting the following error message:

Python Error occurred: workspace already in transaction mode

In the program I have reading cursor and two insert cursors.

curAppend = arcpy.da.InsertCursor(AGREEMENT_I, fieldnamesDelta)
 curUpdate = arcpy.da.InsertCursor(AGREEMENT_U, fieldnamesDelta)


 for  rowToday in curToday:
        agreementID = rowToday[1]
        datetimeVal = rowToday[4]


        if datetimeVal is not None  and agreementID is not None:


            whereClause = '"AGMID" = %s' % (agreementID)


            srcYesterday = arcpy.da.SearchCursor(AGREEMENT, fieldnamesYesterday, whereClause)


            count = 0


            for  rowYesterday in srcYesterday:


                if  rowYesterday[4]  == rowToday[4] :
                    count = count + 1


                    if rowYesterday[5] != rowToday[5] or rowYesterday[6] != rowToday[6] \
                       or rowYesterday[7] != rowToday[7] or rowYesterday[8] != rowToday[8] \
                       or rowYesterday[9] != rowToday[9] or rowYesterday[10] != rowToday[10] \
                       or rowYesterday[11] != rowToday[11] or rowYesterday[12] != rowToday[12] \
                       or rowYesterday[13] != rowToday[13]  or rowYesterday[14] != rowToday[14] :
                            print "Record for Update :", agreementID
                            logging.info('Update ' + whereClause)
                            curUpdate.insertRow(rowToday)


            if count == 0:
                print "Record for Insert :", agreementID
                logging.info('Insert ' + whereClause)
                curAppend.insertRow(rowToday)


.....
0 Kudos
2 Replies
DerekRyter
New Contributor II

Remember that each cursor locks the data that it accesses. I can't see in your code the source of the data in each cursor, but you can't search and insert the same GDB or table. You also have to delete cursors when your done with one to release the lock. This doesn't look like it might happen in your code, but that will trigger the same error.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

there is a whole section on data locks towards the bottom of this link.  It describes the conditions where they might occur and it is given in the context of differences with the old cursors

Accessing data using cursors—Help | ArcGIS for Desktop

from there

In Python, the lock persists until the cursor is released. Otherwise, all other applications or scripts could be unnecessarily prevented from accessing a dataset. A cursor can released by one of the following:

  • Including the cursor inside a with statement, which will guarantee the release of locks regardless of whether or not the cursor is successfully completed
  • Calling reset() on the cursor
  • The completion of the cursor
  • Explicitly deleting the cursor using Python's del statement

An edit session in ArcMap applies a shared lock to data during the edit session. An exclusive lock is applied when edits are saved. A dataset is not editable if an exclusive lock already exists.