Skip to content

Commit

Permalink
Add more debugging to auth db opening
Browse files Browse the repository at this point in the history
(cherry picked from commit ac781fa)
  • Loading branch information
nyalldawson committed Nov 1, 2018
1 parent 31e3a2f commit c068b13
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/core/auth/qgsauthmanager.cpp
Expand Up @@ -30,6 +30,7 @@
#include <QTime>
#include <QTimer>
#include <QVariant>
#include <QSqlDriver>

#include <QtCrypto>

Expand Down Expand Up @@ -112,28 +113,36 @@ QSqlDatabase QgsAuthManager::authDatabaseConnection() const
if ( isDisabled() )
return authdb;

QString connectionName = QStringLiteral( "authentication.configs" );
// 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;
const QString connectionName = QStringLiteral( "authentication.configs:0x%1" ).arg( reinterpret_cast<quintptr>( QThread::currentThread() ), 2 * QT_POINTER_SIZE, 16, QLatin1Char( '0' ) );
QgsDebugMsgLevel( QStringLiteral( "Using auth db connection name: %1 " ).arg( connectionName ), 0 );
if ( !QSqlDatabase::contains( connectionName ) )
{
QgsDebugMsgLevel( QStringLiteral( "No existing connection, creating a new one" ), 0 );
authdb = QSqlDatabase::addDatabase( QStringLiteral( "QSQLITE" ), connectionName );
authdb.setDatabaseName( authenticationDatabasePath() );
// for background threads, remove database when current thread finishes
if ( QThread::currentThread() != QgsApplication::instance()->thread() )
{
QgsDebugMsgLevel( QStringLiteral( "Scheduled auth db remove on thread close" ), 0 );
connect( QThread::currentThread(), &QThread::finished, QThread::currentThread(), [connectionName]
{
QgsDebugMsgLevel( QStringLiteral( "Removing outdated connection to %1 on thread exit" ).arg( connectionName ), 0 );
QSqlDatabase::removeDatabase( connectionName );
} );
}

QgsDebugMsgLevel( QStringLiteral( "Actual DB thread is: 0x%1" ).arg( reinterpret_cast<quintptr>( authdb.driver()->thread() ), 2 * QT_POINTER_SIZE, 16, QLatin1Char( '0' ) ), 0 );
QgsDebugMsgLevel( QStringLiteral( "Actual DB connection name is: %1" ).arg( authdb.connectionName() ), 0 );
}
else
{
QgsDebugMsgLevel( QStringLiteral( "Reusing existing connection" ), 0 );
authdb = QSqlDatabase::database( connectionName );
QgsDebugMsgLevel( QStringLiteral( "Retrieved DB thread is: 0x%1" ).arg( reinterpret_cast<quintptr>( authdb.driver()->thread() ), 2 * QT_POINTER_SIZE, 16, QLatin1Char( '0' ) ), 0 );
QgsDebugMsgLevel( QStringLiteral( "Retrieved DB connection name is: %1" ).arg( authdb.connectionName() ), 0 );
}
if ( !authdb.isOpen() )
{
Expand Down
3 changes: 1 addition & 2 deletions src/providers/mssql/qgsmssqlconnection.cpp
Expand Up @@ -306,6 +306,5 @@ QString QgsMssqlConnection::dbConnectionName( const QString &name )
// 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( QString::number( reinterpret_cast< quintptr >( QThread::currentThread() ), 16 ) );
return name + threadAddress;
return QStringLiteral( "%1:0x%2" ).arg( name, QString::number( reinterpret_cast< quintptr >( QThread::currentThread() ), 16 ) );
}

0 comments on commit c068b13

Please sign in to comment.