Can't release layer lock on geodatabase

2281
10
12-17-2019 02:32 PM
KarenMeinstein
Occasional Contributor

I'm creating a new layer in a map using LayerFactory.Instance.CreateFeatureLayer with an existing layer file previously created with ArcGIS 10.5.1 (i.e., .lyr, .lyrx).  Once this new layer is created, since I know it's data link is broken I change it's data source to a different data source that contains the appropriate data.  This all works fine.  The problem is that when I change the layer's data source, it puts a lock on the entire geodatabase containing the data and now I can't write anything else to the geodatabase.  Even if I close the map containing the layer, the lock remains until I close Pro. Does anyone have any ideas how to release the lock?  Any help would be greatly appreciated.

Here's my code:

Layer newlayer = LayerFactory.Instance.CreateFeatureLayer(symbologyUri, MyMap, LayerPosition.AddToTop);

CIMStandardDataConnection newDataConnection = new CIMStandardDataConnection()
{
WorkspaceConnectionString = workspaceConnectionStringForNewData,
WorkspaceFactory = WorkspaceFactory.FileGDB,
Dataset = datasetName,
DatasetType = esriDatasetType.esriDTFeatureClass
};

newlayer.SetDataConnection(newDataConnection);  //This is where the lock occurs

Tags (1)
10 Replies
KarenMeinstein
Occasional Contributor

I had a Eureka! moment today about this issue while putting together a simple example of the problem for ESRI support.  The failure occurs when the gdal code that is accessing the geodatabase is within the QueryTask.Run call where the data in the same geodatabase is accessed (i.g., LayerFactory.Instance.CreateFeatureLayer()).  If I move the gdal code outside of the QueryTask.Run, it can access the geodatabase with no problem.  This wasn't obvious in my original code because the gdal code was in a method called from within the QueryTask.Run. 

So this code doesn't work:

public async Task RunTest()
{
   await QueuedTask.Run(async () =>
   {
      Map testMap = MapFactory.Instance.CreateMap("Test");
      testMap.SetSpatialReference(SpatialReferences.WGS84);


      Uri layerUri = new Uri(@"C:\Users\kbmeinstein\Desktop\GDBTest\GDBtest.gdb\polygons");

      ArcGIS.Desktop.Mapping.Layer newlayer = LayerFactory.Instance.CreateFeatureLayer(layerUri, testMap,       LayerPosition.AddToBottom);

      await ProApp.Panes.CreateMapPaneAsync(testMap);


        Driver fileGDBDriver = OSGeo.OGR.Ogr.GetDriverByName("FileGDB");

        //Failure happens here:
         DataSource dataSource = fileGDBDriver.Open(@"C:\Users\kbmeinstein\Desktop\GDBTest\GDBtest.gdb", 1);
     });

}

But this does:

public async Task RunTest()
{
   await QueuedTask.Run(async () =>
   {
      Map testMap = MapFactory.Instance.CreateMap("Test");
      testMap.SetSpatialReference(SpatialReferences.WGS84);


      Uri layerUri = new Uri(@"C:\Users\kbmeinstein\Desktop\GDBTest\GDBtest.gdb\polygons");

      ArcGIS.Desktop.Mapping.Layer newlayer = LayerFactory.Instance.CreateFeatureLayer(layerUri, testMap,       LayerPosition.AddToBottom);

      await ProApp.Panes.CreateMapPaneAsync(testMap);
     });

 Driver fileGDBDriver = OSGeo.OGR.Ogr.GetDriverByName("FileGDB");

  //This works fine now:
  DataSource dataSource = fileGDBDriver.Open(@"C:\Users\kbmeinstein\Desktop\GDBTest\GDBtest.gdb", 1);

}

An important thing to note is that in Pro v2.3.1, code like the example above that doesn't work now (i.e., with the gdal within the QueryTask.Run) worked fine, so something definitely changed in v2.3.2.  Hopefully they'll fix this, but at least now I have a workaround and hopefully this is helpful to someone else.