Skip to content

Commit

Permalink
Fix mssql connections not added/removed predictabely when not from ma…
Browse files Browse the repository at this point in the history
…in thread

(cherry picked from commit cf37cb9)
  • Loading branch information
domi authored and nyalldawson committed Nov 20, 2020
1 parent 0a350e1 commit 482d585
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/providers/mssql/qgsmssqlconnection.cpp
Expand Up @@ -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 );
Expand All @@ -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 );
}
}
Expand Down

0 comments on commit 482d585

Please sign in to comment.