AnsweredAssumed Answered

SDE Personal  Database lock

Question asked by CCweedcontrol on Nov 17, 2017
Latest reply on Nov 20, 2017 by CCweedcontrol

I have a ArcSDE Personal geodatabase that i need to edit. I am connecting to the feature class through a save connection other wise i can't edit the feature class. The problem i am having is once i run the code and it runs fine it seem to lock the entire ArcSDE Personal server not just the geodatabase the specific feature class is in.  I thought the 'with' ith arcpy.da.SearchCursor as cur: would release it but it does not. I also tried running the with statement inside of a function to remove the locks on the server when done. I also tried arcpy.ClearWorkspaceCache_management but nothing is working. So i need some help, i am not sure is there is something wrong with my python script. I need to run this script inside Arcmap's python window.


import arcpy, os 

arcpy.env.overwriteOutput = True
arcpy.env.workspace = r'Database Connections\Connection to test.sde'  #Database Connections

fc1 = 'DBO.TaxParcels1' # Taxparcels
fc2 = 'DBOAddresPointsTest_1'

def main():
        lyr2 = arcpy.MakeFeatureLayer_management(fc2, "In_memory\lyr2")
        arcpy.AddField_management(fc2, "Verifi2", "Text", "","",50)
        #build a dictionary of OBJECTID : Address pairs, change OID@ to your uniqueID
        addDict = {row[0]:row[1] for row in arcpy.da.SearchCursor(fc1, ['ACCOUNT','SiteAddress'])}

        # 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)

        # Start an edit operation

        #Spatial joins the points to taxparcels if "Match" is in Verifi2 field, updates points attributes.
        arcpy.env.workspace = r'Database Connections\Connection to test.sde'
        ptSelection = "DBO.AddresPointsTest_1" 
        pointLayer = arcpy.env.workspace + os.sep + "DBO.AddresPointsTest_1"  # DBO.AddresPointsTest
        parcel = 'DBO.TaxParcels1' #Taxparcels
        sjpoints = "In_memory\sjpoints"
        poly = "ACCOUNT_1"
        Pnt =  "Account"

        ptCount = int(arcpy.GetCount_management(pointLayer).getOutput(0))

        dsc = arcpy.Describe(ptSelection)    
        selection_set = dsc.FIDSet            
        if len(selection_set) == 0:          
            print "There are no features selected"       
        elif ptCount >= 1:
            #Run the Spatial Join tool, using the defaults for the join operation and join type
            arcpy.SpatialJoin_analysis(lyr2, parcel, sjpoints)

            # define the field list from the spatial join
            sourceFieldsList = ['ACCOUNT_1', poly,'Field_1','Field_2', 'Field_3','Field_4','Field_5']    #,'StreetType_1'

            # define the field list to the original points
            updateFieldsList = ['Account', Pnt,'Field1', 'Field2', 'Field3', 'Field4','Field4', 'Field5'] #, 'StreetType'

            # populate the dictionary from the polygon
            valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sjpoints, sourceFieldsList)}   
            whereclause = "Verifi2 = 'Match'"
            with arcpy.da.UpdateCursor(ptSelection, updateFieldsList ,whereclause) as updateRows:   
                for updateRow in updateRows:   
                    keyValue = updateRow[0].title()
                    if keyValue in valueDict:
                        for n in range (1,len(sourceFieldsList)):     
                            updateRow[n] = valueDict[keyValue][n-1]
                del valueDict
                del updateRows
                del updateRow

        # Stop the edit operation.
        # Stop the edit session and save the changes

        #Clear geodatabase locks.
        # Set environment settings
        arcpy.env.workspace = "Database Connections\Connection to Test.sde" # Creates a connection to enterprise geodatabase
        fcList = arcpy.ListFeatureClasses() # Show that we are connected
        print(str(fcList) + "\n")
        arcpy.env.workspace = "" # Release hold on enterprise geodatabase workspace created in previous step.

        # Execute the Clear Workspace Cache tool
        arcpy.ClearWorkspaceCache_management("Database Connections\Connection to Test.sde")
        print(arcpy.GetMessages() + "\n")
       arcpy.AddError("Python Messages: " + arcpy.GetMessages())
if __name__ == '__main__':