Skip to content
Permalink
Browse files

[mssql] Don't share connections between threads

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@a3814e3
  • Loading branch information
nyalldawson committed Oct 2, 2018
1 parent 57f77d0 commit 3b8a2d94c29ff2189bed2c746e34635598098009
Showing with 6 additions and 0 deletions.
  1. +6 −0 src/providers/mssql/qgsmssqlprovider.cpp
@@ -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 );

0 comments on commit 3b8a2d9

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