[gdal-dev] Attempt to delete layer fails - table locked

Even Rouault even.rouault at spatialys.com
Mon Sep 18 00:40:08 PDT 2017


Nik,

You didn't mention which GDAL version you use, and the file format (from the error message, 
I presume it is GeoPackage)

I didn't manage to reproduce with trunk, 2.2 or 2.1 or even 2.0 and the following actions:

$ ogr2ogr -f gpkg poly.gpkg poly.shp
$ python
from osgeo import ogr
ds = ogr.Open('poly.gpkg', update = 1)
sql_lyr = ds.ExecuteSQL('SELECT * from poly')
lyr = ds.GetLayer(0)
lyr.CreateFeature(ogr.Feature(lyr.GetLayerDefn()))
lyr  = None
ds.DeleteLayer(0)
ds = None

DeleteLayer() normally cleans up the read and write sqlite3 statements still active on that 
layer

But if you do:
from osgeo import ogr
ds = ogr.Open('poly.gpkg', update = 1)
sql_lyr = ds.ExecuteSQL('SELECT * from poly') <-- new command added
lyr = ds.GetLayer(0)
lyr.CreateFeature(ogr.Feature(lyr.GetLayerDefn()))
lyr  = None
ds.DeleteLayer(0)
ds = None

I do get those errors since the ExecuteSQL() layer still holds a sqlite3 statement that the 
layer isn't aware of.

Another variation that gives the error:

$ ogr2ogr -f gpkg poly.gpkg poly.shp
$ ogr2ogr -update poly.gpkg poly.shp -nln another_layer
$ python
from osgeo import ogr
ds = ogr.Open('poly.gpkg', update = 1)

lyr2 = ds.GetLayerByName('another_layer')
lyr2.GetNextFeature() <-- will start a new sqlite3 statement

lyr = ds.GetLayerByName('poly')
lyr.CreateFeature(ogr.Feature(lyr.GetLayerDefn()))
lyr  = None

ds.DeleteLayer(0)
ds = None

So any pending sqlite3 statement, not only those on the layer, prevents the table from being 
deleted. I presume your complete use case must be close to the 2 last above cases

The workaround is to explicitly call ResetReading() on all layers that you have read and/or 
write, and ReleaseResultSet() on all layers returned by ExecuteSQL().
I'm pondering whether the driver shouldn't do that automatically (of course that wouldn't 
help if another dataset object is opened on the same file in the same or a different process)


Een

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20170918/c85cf4af/attachment.html>


More information about the gdal-dev mailing list