Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[db-manager] Invalidate sqlite connection if it was created in anothe…
…r thread

Fixes #21028 - DB manager: SQLite objects created in a thread can only be used in that same thread
  • Loading branch information
elpaso committed Mar 11, 2019
1 parent cabf077 commit b0b068d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions python/plugins/db_manager/db_plugins/gpkg/connector.py
Expand Up @@ -25,6 +25,7 @@
from functools import cmp_to_key

from qgis.PyQt.QtWidgets import QApplication
from qgis.PyQt.QtCore import QThread

from ..connector import DBConnector
from ..plugin import ConnectionError, DbError, Table
Expand Down Expand Up @@ -63,6 +64,26 @@ def _opendb(self):
raise ConnectionError(QApplication.translate("DBManagerPlugin", '"{0}" not found').format(self.dbname))
self.has_raster = self.gdal_ds.RasterCount != 0 or self.gdal_ds.GetMetadata('SUBDATASETS') is not None
self.connection = None
self._current_thread = None

@property
def connection(self):
"""Creates and returns a spatialite connection, if
the existing connection was created in another thread
invalidates it and create a new one.
"""

if self._connection is None or self._current_thread != int(QThread.currentThreadId()):
self._current_thread = int(QThread.currentThreadId())
try:
self._connection = spatialite_connect(str(self.dbname))
except self.connection_error_types() as e:
raise ConnectionError(e)
return self._connection

@connection.setter
def connection(self, conn):
self._connection = conn

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

0 comments on commit b0b068d

Please sign in to comment.