Monday, 24 September 2018

arcgis desktop - Debugging RuntimeError: workspace already in transaction mode from arcpy.da.UpdateCursor and ArcSDE feature classes?


I am making my first attempt at editing an ArcSDE feature class with python through a da.UpdateCursor. I'm essentially taking code I've written for a file geodatabase feature class and applying it to an SDE feature class. Doing so produces an error, and I'm not sure how to rectify the problem.


I'm using ArcGIS 10.1 for Desktop.


Pertinent code:


Struxfeature = r"DatabaseConnections\PipelinePathways.sde\PPGIS.Strux\PPGIS.StruxPts_v04_all"

P6feature = r"DatabaseConnections\PipelinePathways.sde\PPGIS.Strux\PPGIS.Land_Projects_Parcels_P6"

SDE = r"Database Connections\PipelinePathways.sde"
UserID = "E1B8"
Parent = "SDE.DEFAULT"
version = "change_RW_VC_4447_14_to_C"

#Create Version
print "Creating version"
arcpy.CreateVersion_management(SDE, Parent, version, "PUBLIC")

VersionName = UserID.upper() + "." + version

#Layers
arcpy.MakeFeatureLayer_management (Struxfeature, "Struxlyr")
arcpy.MakeFeatureLayer_management (P6feature, "P6lyr")

#Switch to version
print "Switching version"
arcpy.ChangeVersion_management("Struxlyr", "TRANSACTIONAL", VersionName)
arcpy.ChangeVersion_management("P6lyr", "TRANSACTIONAL", VersionName)


#Start editing
print "Initiating editing"
edit = arcpy.da.Editor(SDE)
edit.startEditing()

# Start an edit operation
edit.startOperation()



#Change P6 project numbers
print "Updating P6.\n"
P6Cursor = arcpy.da.UpdateCursor ("P6lyr", ["P6_NBR", "Name"])
for row in P6Cursor:
codecodecode

The error comes from the line 'for row in P6Cursor:'


Traceback (most recent call last):
File "C:\E1B8\ScriptTesting\ScriptsIUse\ChangeP6.py", line 81, in
for row in P6Cursor:

RuntimeError: workspace already in transaction mode

Answer



With help I have found my solution, though the reasoning behind it is a bit blurry currently. In my code, creating a version through one SDE connection file and then creating the feature layer to be edited through another SDE connection file that is connected to the new version works. Also, edit.startEditing must have the variable 'multiuser_mode' set to true (the default). As I understand it, this variable indicates whether there can be multiple versions/users allowed for the layer. Since this is always true for an SDE feature layer, setting this variable to False causes an error.


Functioning code, where I test an update cursor:


import arcpy
import os
#Locals
P6featureName = r"PPGIS.Strux\PPGIS.Land_Projects_Parcels_P6"
Parent = "SDE.DEFAULT"
version = "SDE_Test"

Server = ***
Service = ***
user = ***
Pass = ***
SDE = "Database Connections\PipelinePathways.sde"
temploc = r"C:\E1B8\ScriptTesting\Workspace"
fil = "SDETempConn"

env.overwriteOutput = True


#Create Version
print "Creating version"
arcpy.CreateVersion_management (SDE, Parent, version, "PUBLIC")
VersionName = user.upper() + "." + version

#Create new connection
workspace = os.path.join (temploc, fil + ".sde")
print "Creating SDE connection"
arcpy.CreateArcSDEConnectionFile_management (temploc, fil, Server, Service, username = user, password = Pass, version = VersionName)


#Layers
P6feature = os.path.join (workspace, P6featureName)
arcpy.MakeFeatureLayer_management (P6feature, "P6lyr")

#Start editing
print "Initiating editing"
edit = arcpy.da.Editor (workspace)
edit.startEditing ()
edit.startOperation()


#Test Cursor
print "Testing cursor"
P6Cursor = arcpy.da.UpdateCursor ("P6lyr", ["NAME"])
for row in P6Cursor:
print row[0]
del row
del P6Cursor

#Stop/save edits
edit.stopOperation()

print "Stopping editing"
edit.stopEditing("True")

#Switch to version
print "Switching version"
arcpy.ChangeVersion_management("P6lyr", "TRANSACTIONAL", Parent)

#Reconcile and post
print "Reconciling and posting"
arcpy.ReconcileVersions_management (workspace, "", Parent, VersionName, with_post = "POST", with_delete = "DELETE_VERSION")


Thanks to Ben Nadler for the help.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...