Select to view content in your preferred language

Editing a Versioned SDE FC using python - Error

1759
1
04-23-2019 05:59 AM
BrittanBattles3
New Contributor

Getting the following error below when trying to edit a versioned feature class in an SDE database using a python script tool:

Traceback (most recent call last):
File "L:\Tools\Models\ReconcilePostCalcFields&RecreateVersions.py", line 32, in <module>
for row in cursor:
RuntimeError: workspace already in transaction mode

Code is below:

import arcpy, os
# Set workspace
workspace = "Database Connections\\Connection to gis-prod.sde"
# Set the workspace environment
arcpy.env.workspace = workspace
# Create Version using SDE credentials
arcpy.CreateVersion_management (workspace, "sde.default", "fieldcalcs", "PUBLIC")
# Delcare variables
##versionList = arcpy.ListVersions('Database Connections\\Connection to gis-prod.sde')
inFeatures = "sde_m.SDE.PARCEL"
zip5expression = "!MAIL_ZIP![0:5]"
fl = "fl"
# Change Workspace to access data via OS Auth.
workspace2 = "Database Connections\\SDE_MPL_PROD_OSAUTH.sde"
arcpy.env.workspace = workspace2
# Create Feature Layer for editing
arcpy.MakeFeatureLayer_management (inFeatures, fl)
# Change Version
arcpy.ChangeVersion_management (fl,"TRANSACTIONAL", "sde.fieldcalcs")
# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(workspace2)
# Edit session is started without an undo/redo stack for versioned data
edit.startEditing(False, True)
# Start an edit operation
edit.startOperation()
# Calculate the MAIL_ZIP5 field
fields = ['MAIL_ZIP5']
with arcpy.da.UpdateCursor(fl, fields) as cursor:
	for row in cursor:
		row[0] = zip5expression
		cursor.updateRow(row)	
# Stop the edit operation.
edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

If I comment out the UpdateCursor section, the script runs successfully. I have also tried to reconcile, post and delete the child versions before executing the script with the same failure, all locks on the database are shared locks. I thought by versioning and editing, I could get around any locking issues. Any idea how to get the workspace out of transaction mode to allow editing to the child version created in the script?

0 Kudos
1 Reply
BirajaNayak
Esri Contributor

This is a known issue and defect number is #BUG-000114598. It states that "RuntimeError "workspace already in transaction mode" occurs when using da.UpdateCursor on TableView after changing the version."

Workaround steps are provided below:

1. Change the connection file to point to the desired version.
2. Use the CreateDatabaseConnection_management tool to generate a temporary connection file pointing to the desired version. Delete when the script finishes.
3. Use a config file to store the arguments for the CreateDatabaseConnection tool to keep the username and password out of the script.(optional)

This is a code snippet used in another user case with similar situation:

At the beginning of your script:

temp = r"C:\Temp"
file = "SDEConn"
arcpy.CreateArcSDEConnectionFile_management(temp, file, <Fill In The Rest With Your RBDMS Parameters>)
workspace = os.path.join(temp, file +".sde")
# Set the workspace environment
env.workspace = workspace


At the end of your script:
arcpy.Delete(workspace)

Hope that helps.

Biraja

0 Kudos