SDE copy feature to FGDB, subsequent Near tool execution fails with schema lock error

Discussion created by matthew.barlow on Feb 7, 2014
Latest reply on Feb 7, 2014 by matthew.barlow
I am attempting to run the following code (more or less, as I have simplified it for display here):

arcpy.env.workspace = ws
arcpy.env.overwriteOutput = True

arcpy.MakeFeatureLayer_management(sde_fc, gdb_fc)
arcpy.SelectLayerByLocation_management(gdb_fc, 'WITHIN_A_DISTANCE', gdb_asset_name, "1000")

matchcount = int(arcpy.GetCount_management(gdb_fc).getOutput(0))
if matchcount <> 0:
     arcpy.CopyFeatures_management(gdb_fc, ws + "\\" + gdb_fc)
arcpy.Near_analysis(gdb_fc, gdb_asset_name, "#", "#", "ANGLE")

It functions as expected when copying the selected features from a file geodatabase or shapefiles. However, when copying from SDE, it fails and returns:

ERROR 000464: Cannot get exclusive schema lock.  Either being edited or in use by another application.
Lock request conflicts with an established lock

To complicate matters, it isn't consistent with all SDE databases. On some it functions correctly, but not on the one I need to point to. Could this be a lock carrying over from SDE in the FGDB?