Database exclusive schema lock

1820
4
Jump to solution
09-02-2020 10:09 AM
TonyAlmeida
Occasional Contributor II

I updated to ArcPro 2.6 (python 3.6.10) and after I noticed  that one of my tools/scrips now giving the following error. I am assuming something from the ArcPro 2.6 python 3.6.10 is now throwing it off but I am not sure what. I did not change any of the code. I tried change workspace to from r"Database Connections\Connection to Blah" to " C:\Users\***\AppData\Roaming\ESRI\Desktop10.6\ArcCatalog\***.gds" but that didn't work. Can someone shine in as to why the code is failing after the upgrade to ArcPro 2.6?

The database is in a ArcSDE Personal SQL Server

ERROR 000464: Cannot get exclusive schema lock.  Either being edited or in use by another application.
Failed to execute (CopyFeatures).

import arcpy, os
from arcpy import env

fc = "Blah.DBO.Blah"
arcpy.env.workspace = r"Database Connections\Connection to Blah"
pointLayer = arcpy.env.workspace + os.sep + "Blah" #target point feature class 
# Start an edit session. Must provide the worksapce.
edit = arcpy.da.Editor(arcpy.env.workspace)

# Edit session is started without an undo/redo stack for versioned data
#  (for second argument, use False for unversioned data)
edit.startEditing(True)

# Start an edit operation
edit.startOperation()

input = arcpy.GetParameterAsText(0)
Range = float(arcpy.GetParameterAsText(1))
# Create empty Point and Array objects
point = arcpy.Point()
array = arcpy.Array()

# A list that will hold each the Polyline object
featureList = []

rows = arcpy.SearchCursor(input)
for row in rows:
    geom = row.Shape
    point.X = geom.centroid.X
    point.Y = geom.centroid.Y
    # Add each point to the array
    array.add(point)    

# Create the polyline
polyline = arcpy.Polyline(array)
# Clear the array for future use
array.removeAll()
featureList.append(polyline)

del row, rows

# Copy the polyline to file geodatabase to create a Shape_Length field
arcpy.CopyFeatures_management(featureList, "line_test")
rows = arcpy.SearchCursor("line_test")
for row in rows:
        geom = row.shape
        X = geom.lastPoint.X
        Y = geom.lastPoint.Y
        length = row.Shape_Length / 5.28 + Range
del row, rows
row_value = (length, (X, Y))
        
cursor = arcpy.da.InsertCursor(fc, ("SiteNum", "SHAPE@XY"))
cursor.insertRow(row_value)

#arcpy.Delete_management("line_test")

# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(True)

arcpy.RefreshActiveView()
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I did try to add a create database connection but I get the following error.

edit = arcpy.da.Editor(arcpy.env.workspace)
RuntimeError: cannot open workspace

arcpy.env.overwriteOutput = True
out_folder_path = r"C:\Temp"
out_name = "Connect.sde"
platform = "SQL_SERVER"
instance = "SQL_SERVER"
database = r"Database Servers\***.gds\blah (VERSION:dbo.DEFAULT)"
arcpy.CreateDatabaseConnection_management(out_folder_path,
                                          out_name,
                                          platform,
                                          instance,
                                          "OPERATING_SYSTEM_AUTH",
                                          "#",
                                          "#",
                                          "#",
                                          database)
print(out_name)

workspace = os.path.join(out_folder_path, out_name +".sde")
# Set the workspace environment
arcpy.env.workspace = workspace
print(workspace)

fc = "Blah.DBO.Blah"
arcpy.env.workspace = r"Database Connections\Connection to Blah"
pointLayer = arcpy.env.workspace + os.sep + "Blah" #target point feature class 
# Start an edit session. Must provide the worksapce.
edit = arcpy.da.Editor(arcpy.env.workspace)

# Edit session is started without an undo/redo stack for versioned data
#  (for second argument, use False for unversioned data)
edit.startEditing(True)

# Start an edit operation
edit.startOperation()

input = arcpy.GetParameterAsText(0)
Range = float(arcpy.GetParameterAsText(1))
# Create empty Point and Array objects
point = arcpy.Point()
array = arcpy.Array()

# A list that will hold each the Polyline object
featureList = []

rows = arcpy.SearchCursor(input)
for row in rows:
    geom = row.Shape
    point.X = geom.centroid.X
    point.Y = geom.centroid.Y
    # Add each point to the array
    array.add(point)    

# Create the polyline
polyline = arcpy.Polyline(array)
# Clear the array for future use
array.removeAll()
featureList.append(polyline)

del row, rows

# Copy the polyline to file geodatabase to create a Shape_Length field
arcpy.CopyFeatures_management(featureList, "line_test")
rows = arcpy.SearchCursor("line_test")
for row in rows:
        geom = row.shape
        X = geom.lastPoint.X
        Y = geom.lastPoint.Y
        length = row.Shape_Length / 5.28 + Range
del row, rows
row_value = (length, (X, Y))
        
cursor = arcpy.da.InsertCursor(fc, ("SiteNum", "SHAPE@XY"))
cursor.insertRow(row_value)

#arcpy.Delete_management("line_test")

# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(True)

arcpy.RefreshActiveView()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

Search cursors also support with statements to reset iteration and aid in removal of locks.

Comments on the data access cursors can be found in their various help topics.

They do perform better


... sort of retired...

View solution in original post

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

you are mixing old and new cursors... might be an issue

old

rows = arcpy.SearchCursor(input)

new

SearchCursor—ArcGIS Pro | Documentation 

with arcpy.da.SearchCursor(fc, fields) as cursor:

    etc


... sort of retired...
0 Kudos
TonyAlmeida
Occasional Contributor II

Why would a searchCursor cause a exclusive schema lock? It worked prior to updating to Pro 2.6.

0 Kudos
DanPatterson
MVP Esteemed Contributor

Search cursors also support with statements to reset iteration and aid in removal of locks.

Comments on the data access cursors can be found in their various help topics.

They do perform better


... sort of retired...
0 Kudos
TonyAlmeida
Occasional Contributor II

I see I guess I didn't see that but good to know.

I changed part of the code to  and got pasted the schema lock but now I am facing that the Line_teast already exists.

Even through I have arcpy.env.overwriteOutput = True.

input = arcpy.GetParameterAsText(0)
Range = float(arcpy.GetParameterAsText(1))
# Create empty Point and Array objects
point = arcpy.Point()
array = arcpy.Array()

# A list that will hold each the Polyline object
featureList = []

with arcpy.da.SearchCursor(input, "SHAPE@") as cursor1:
    for row in cursor1:
        #geom = row.Shape
        point.X = row[0].centroid.X
        point.Y = row[0].centroid.Y
        # Add each point to the array
        array.add(point)    

# Create the polyline
polyline = arcpy.Polyline(array)
# Clear the array for future use
array.removeAll()
featureList.append(polyline)

del row

# Copy the polyline to file geodatabase to create a Shape_Length field
arcpy.CopyFeatures_management(featureList, "line_test")

with arcpy.da.SearchCursor("line_test", ["SHAPE@", "SHAPE@LENGTH"]) as cursor1:
    for row in cursor1:
            #geom = row.shape
            X = row[0].lastPoint.X
            Y = row[0].lastPoint.Y
            length = row[1] / 5.28 + Range
    del row
row_value = (length, (X, Y))
        
cursor = arcpy.da.InsertCursor(pointLayer, ("SiteNum", "SHAPE@XY"))
cursor.insertRow(row_value)

Changed to

0 Kudos