Skip to content

Commit b0b068d

Browse files
committed
[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 cabf077 commit b0b068d

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
@@ -63,6 +64,26 @@ def _opendb(self):
6364
raise ConnectionError(QApplication.translate("DBManagerPlugin", '"{0}" not found').format(self.dbname))
6465
self.has_raster = self.gdal_ds.RasterCount != 0 or self.gdal_ds.GetMetadata('SUBDATASETS') is not None
6566
self.connection = None
67+
self._current_thread = None
68+
69+
@property
70+
def connection(self):
71+
"""Creates and returns a spatialite connection, if
72+
the existing connection was created in another thread
73+
invalidates it and create a new one.
74+
"""
75+
76+
if self._connection is None or self._current_thread != int(QThread.currentThreadId()):
77+
self._current_thread = int(QThread.currentThreadId())
78+
try:
79+
self._connection = spatialite_connect(str(self.dbname))
80+
except self.connection_error_types() as e:
81+
raise ConnectionError(e)
82+
return self._connection
83+
84+
@connection.setter
85+
def connection(self, conn):
86+
self._connection = conn
6687

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

0 commit comments

Comments
 (0)