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.
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'Database Connections\Connection to test.sde'</SPAN> <SPAN class="comment token">#Database Connections</SPAN>
fc1 <SPAN class="operator token">=</SPAN> <SPAN class="string token">'DBO.TaxParcels1'</SPAN> <SPAN class="comment token"># Taxparcels</SPAN>
fc2 <SPAN class="operator token">=</SPAN> <SPAN class="string token">'DBOAddresPointsTest_1'</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
lyr2 <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>fc2<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"In_memory\lyr2"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>fc2<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Verifi2"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Text"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">50</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#build a dictionary of OBJECTID : Address pairs, change OID@ to your uniqueID</SPAN>
addDict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>fc1<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'ACCOUNT'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'SiteAddress'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">}</SPAN>
<SPAN class="comment token"># Start an edit session. Must provide the worksapce.</SPAN>
edit <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>Editor<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Edit session is started without an undo/redo stack for versioned data</SPAN>
<SPAN class="comment token"># (for second argument, use False for unversioned data)</SPAN>
edit<SPAN class="punctuation token">.</SPAN>startEditing<SPAN class="punctuation token">(</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Start an edit operation</SPAN>
edit<SPAN class="punctuation token">.</SPAN>startOperation<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Spatial joins the points to taxparcels if "Match" is in Verifi2 field, updates points attributes.</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'Database Connections\Connection to test.sde'</SPAN>
ptSelection <SPAN class="operator token">=</SPAN> <SPAN class="string token">"DBO.AddresPointsTest_1"</SPAN>
pointLayer <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">+</SPAN> os<SPAN class="punctuation token">.</SPAN>sep <SPAN class="operator token">+</SPAN> <SPAN class="string token">"DBO.AddresPointsTest_1"</SPAN> <SPAN class="comment token"># DBO.AddresPointsTest</SPAN>
parcel <SPAN class="operator token">=</SPAN> <SPAN class="string token">'DBO.TaxParcels1'</SPAN> <SPAN class="comment token">#Taxparcels</SPAN>
sjpoints <SPAN class="operator token">=</SPAN> <SPAN class="string token">"In_memory\sjpoints"</SPAN>
poly <SPAN class="operator token">=</SPAN> <SPAN class="string token">"ACCOUNT_1"</SPAN>
Pnt <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Account"</SPAN>
ptCount <SPAN class="operator token">=</SPAN> int<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management<SPAN class="punctuation token">(</SPAN>pointLayer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>getOutput<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
dsc <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>ptSelection<SPAN class="punctuation token">)</SPAN>
selection_set <SPAN class="operator token">=</SPAN> dsc<SPAN class="punctuation token">.</SPAN>FIDSet
<SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>selection_set<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"There are no features selected"</SPAN>
<SPAN class="keyword token">elif</SPAN> ptCount <SPAN class="operator token">>=</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#Run the Spatial Join tool, using the defaults for the join operation and join type</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SpatialJoin_analysis<SPAN class="punctuation token">(</SPAN>lyr2<SPAN class="punctuation token">,</SPAN> parcel<SPAN class="punctuation token">,</SPAN> sjpoints<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># define the field list from the spatial join</SPAN>
sourceFieldsList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'ACCOUNT_1'</SPAN><SPAN class="punctuation token">,</SPAN> poly<SPAN class="punctuation token">,</SPAN><SPAN class="string token">'Field_1'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'Field_2'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Field_3'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'Field_4'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'Field_5'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token">#,'StreetType_1'</SPAN>
<SPAN class="comment token"># define the field list to the original points</SPAN>
updateFieldsList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Account'</SPAN><SPAN class="punctuation token">,</SPAN> Pnt<SPAN class="punctuation token">,</SPAN><SPAN class="string token">'Field1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Field2'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Field3'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Field4'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'Field4'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Field5'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token">#, 'StreetType'</SPAN>
<SPAN class="comment token"># populate the dictionary from the polygon</SPAN>
valueDict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN>r<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">(</SPAN>r<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> r <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>sjpoints<SPAN class="punctuation token">,</SPAN> sourceFieldsList<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">}</SPAN>
whereclause <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Verifi2 = 'Match'"</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>ptSelection<SPAN class="punctuation token">,</SPAN> updateFieldsList <SPAN class="punctuation token">,</SPAN>whereclause<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> updateRows<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> updateRow <SPAN class="keyword token">in</SPAN> updateRows<SPAN class="punctuation token">:</SPAN>
keyValue <SPAN class="operator token">=</SPAN> updateRow<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>title<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> keyValue <SPAN class="keyword token">in</SPAN> valueDict<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> n <SPAN class="keyword token">in</SPAN> range <SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>len<SPAN class="punctuation token">(</SPAN>sourceFieldsList<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
updateRow<SPAN class="punctuation token">[</SPAN>n<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> valueDict<SPAN class="punctuation token">[</SPAN>keyValue<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN>n<SPAN class="number token">-1</SPAN><SPAN class="punctuation token">]</SPAN>
updateRows<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>updateRow<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">del</SPAN> valueDict
<SPAN class="keyword token">del</SPAN> updateRows
<SPAN class="keyword token">del</SPAN> updateRow
<SPAN class="comment token"># Stop the edit operation.</SPAN>
edit<SPAN class="punctuation token">.</SPAN>stopOperation<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Stop the edit session and save the changes</SPAN>
edit<SPAN class="punctuation token">.</SPAN>stopEditing<SPAN class="punctuation token">(</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Clear geodatabase locks.</SPAN>
<SPAN class="comment token"># Set environment settings</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Database Connections\Connection to Test.sde"</SPAN> <SPAN class="comment token"># Creates a connection to enterprise geodatabase</SPAN>
fcList <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFeatureClasses<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Show that we are connected</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>fcList<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN> <SPAN class="comment token"># Release hold on enterprise geodatabase workspace created in previous step.</SPAN>
<SPAN class="comment token"># Execute the Clear Workspace Cache tool</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>ClearWorkspaceCache_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Database Connections\Connection to Test.sde"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Python Messages: "</SPAN> <SPAN class="operator token">+</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN>
main<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>