Skip to content

Commit 8fb112d

Browse files
committedMar 11, 2019
[db-manager] Invalidate sqlite connection if it was created in another thread
Fixes #21028 - DB manager: SQLite objects created in a thread can only be used in that same thread
1 parent 8f0cfda commit 8fb112d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
 

‎python/plugins/db_manager/db_plugins/gpkg/connector.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from functools import cmp_to_key
2626

2727
from qgis.PyQt.QtWidgets import QApplication
28+
from qgis.PyQt.QtCore import QThread
2829

2930
from ..connector import DBConnector
3031
from ..plugin import ConnectionError, DbError, Table
@@ -65,6 +66,26 @@ def _opendb(self):
6566
raise ConnectionError(QApplication.translate("DBManagerPlugin", '"{dbname}" not recognized as GPKG ({shortname} reported instead.)').format(dbname=self.dbname, shortname=self.gdal_ds.GetDriver().ShortName))
6667
self.has_raster = self.gdal_ds.RasterCount != 0 or self.gdal_ds.GetMetadata('SUBDATASETS') is not None
6768
self.connection = None
69+
self._current_thread = None
70+
71+
@property
72+
def connection(self):
73+
"""Creates and returns a spatialite connection, if
74+
the existing connection was created in another thread
75+
invalidates it and create a new one.
76+
"""
77+
78+
if self._connection is None or self._current_thread != int(QThread.currentThreadId()):
79+
self._current_thread = int(QThread.currentThreadId())
80+
try:
81+
self._connection = spatialite_connect(str(self.dbname))
82+
except self.connection_error_types() as e:
83+
raise ConnectionError(e)
84+
return self._connection
85+
86+
@connection.setter
87+
def connection(self, conn):
88+
self._connection = conn
6889

6990
def unquoteId(self, quotedId):
7091
if len(quotedId) <= 2 or quotedId[0] != '"' or quotedId[len(quotedId) - 1] != '"':

0 commit comments

Comments
 (0)
Please sign in to comment.