Skip to content

Commit

Permalink
[mssql] Don't share connections between threads
Browse files Browse the repository at this point in the history
Starting with Qt 5.11, sharing the same connection between
threads is not allowed. Use a dedicated connection for each
thread requiring access to the database, using the thread
address as connection name.

https://bugreports.qt.io/browse/QTBUG-68486 indicates that
this change is by design and that sharing connections between
threads was always unsafe.

Fixes broken mssql provider on Qt 5.11, and possibly who know
what issues on earlier Qt.

Ported from mumble-voip/mumble@a3814e33
  • Loading branch information
nyalldawson committed Oct 2, 2018
1 parent 57f77d0 commit 3b8a2d9
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/providers/mssql/qgsmssqlprovider.cpp
Expand Up @@ -233,6 +233,12 @@ QSqlDatabase QgsMssqlProvider::GetDatabase( const QString &service, const QStrin
else
connectionName = service;

// Starting with Qt 5.11, sharing the same connection between threads is not allowed.
// We use a dedicated connection for each thread requiring access to the database,
// using the thread address as connection name.
const QString threadAddress = QStringLiteral( ":0x%1" ).arg( reinterpret_cast< quintptr >( QThread::currentThreadId() ), 16 );
connectionName += threadAddress;

if ( !QSqlDatabase::contains( connectionName ) )
{
db = QSqlDatabase::addDatabase( QStringLiteral( "QODBC" ), connectionName );
Expand Down

0 comments on commit 3b8a2d9

Please sign in to comment.