Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4ce65d2

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 aba1e30 commit 4ce65d2

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.