Skip to content
Permalink
Browse files

Fix mssql connections not added/removed predictabely when not from ma…

…in thread

(cherry picked from commit cf37cb9)
  • Loading branch information
domi authored and nyalldawson committed Nov 20, 2020
1 parent 0a350e1 commit 482d5851e5188dfe62d67162d14f0cf685907f0b
Showing with 4 additions and 4 deletions.
  1. +4 −4 src/providers/mssql/qgsmssqlconnection.cpp
@@ -51,12 +51,12 @@ QSqlDatabase QgsMssqlConnection::getDatabase( const QString &service, const QStr
else
connectionName = service;

const QString threadSafeConnectionName = dbConnectionName( connectionName );

// while everything we use from QSqlDatabase here is thread safe, we need to ensure
// that the connection cleanup on thread finalization happens in a predictable order
QMutexLocker locker( &sMutex );

const QString threadSafeConnectionName = dbConnectionName( connectionName );

if ( !QSqlDatabase::contains( threadSafeConnectionName ) )
{
db = QSqlDatabase::addDatabase( QStringLiteral( "QODBC" ), threadSafeConnectionName );
@@ -73,10 +73,10 @@ QSqlDatabase QgsMssqlConnection::getDatabase( const QString &service, const QStr
// and a subsequent call to QSqlDatabase::database with the same thread address (yep it happens, actually a lot)
// triggers a condition in QSqlDatabase which detects the nullptr private thread data and returns an invalid database instead.
// QSqlDatabase::removeDatabase is thread safe, so this is ok to do.
QObject::connect( QThread::currentThread(), &QThread::finished, QThread::currentThread(), [connectionName]
QObject::connect( QThread::currentThread(), &QThread::finished, QThread::currentThread(), [threadSafeConnectionName]
{
QMutexLocker locker( &sMutex );
QSqlDatabase::removeDatabase( connectionName );
QSqlDatabase::removeDatabase( threadSafeConnectionName );
}, Qt::DirectConnection );
}
}

0 comments on commit 482d585

Please sign in to comment.
You can’t perform that action at this time.