|
41 | 41 | #include "keychain.h"
|
42 | 42 |
|
43 | 43 | // QGIS includes
|
| 44 | +#include "qgsapplication.h" |
44 | 45 | #include "qgsauthcertutils.h"
|
45 | 46 | #include "qgsauthcrypto.h"
|
46 | 47 | #include "qgsauthmethod.h"
|
@@ -111,15 +112,28 @@ QSqlDatabase QgsAuthManager::authDatabaseConnection() const
|
111 | 112 | if ( isDisabled() )
|
112 | 113 | return authdb;
|
113 | 114 |
|
114 |
| - QString connectionname = QStringLiteral( "authentication.configs" ); |
115 |
| - if ( !QSqlDatabase::contains( connectionname ) ) |
| 115 | + QString connectionName = QStringLiteral( "authentication.configs" ); |
| 116 | + // Sharing the same connection between threads is not allowed. |
| 117 | + // We use a dedicated connection for each thread requiring access to the database, |
| 118 | + // using the thread address as connection name. |
| 119 | + const QString threadAddress = QStringLiteral( ":0x%1" ).arg( reinterpret_cast< quintptr >( QThread::currentThreadId() ), 16 ); |
| 120 | + connectionName += threadAddress; |
| 121 | + if ( !QSqlDatabase::contains( connectionName ) ) |
116 | 122 | {
|
117 |
| - authdb = QSqlDatabase::addDatabase( QStringLiteral( "QSQLITE" ), connectionname ); |
| 123 | + authdb = QSqlDatabase::addDatabase( QStringLiteral( "QSQLITE" ), connectionName ); |
118 | 124 | authdb.setDatabaseName( authenticationDatabasePath() );
|
| 125 | + // for background threads, remove database when current thread finishes |
| 126 | + if ( QThread::currentThread() != QgsApplication::instance()->thread() ) |
| 127 | + { |
| 128 | + connect( QThread::currentThread(), &QThread::finished, QThread::currentThread(), [connectionName] |
| 129 | + { |
| 130 | + QSqlDatabase::removeDatabase( connectionName ); |
| 131 | + } ); |
| 132 | + } |
119 | 133 | }
|
120 | 134 | else
|
121 | 135 | {
|
122 |
| - authdb = QSqlDatabase::database( connectionname ); |
| 136 | + authdb = QSqlDatabase::database( connectionName ); |
123 | 137 | }
|
124 | 138 | if ( !authdb.isOpen() )
|
125 | 139 | {
|
|
0 commit comments