Results 1 to 4 of 4

Thread: requested database does not belong to the calling thread

  1. #1
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default requested database does not belong to the calling thread

    I tried to compile my application for qt 5.12 and i get the error from title
    Qt Code:
    1. QSqlDatabasePrivate::database: requested database does not belong to the calling thread.
    To copy to clipboard, switch view to plain text mode 
    The thing is that my application was already creating a new connection in every thread that uses database,
    and i cloned default connection to a new one for the calling thread, like this:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::cloneDatabase(QSqlDatabase::database(), name);
    To copy to clipboard, switch view to plain text mode 

    With this new restriction I cant clone database from another thread. How am i supposed to do it now

  2. #2
    Join Date
    Feb 2016
    Posts
    19
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: requested database does not belong to the calling thread

    Hi

    You can try my DataBaseManager
    Qt Code:
    1. /*
    2.  * This file is part of QRK - Qt Registrier Kasse
    3.  *
    4.  * Copyright (C) 2015-2018 Christian Kvasny <chris@ckvsoft.at>
    5.  *
    6.  * This program is free software; you can redistribute it and/or modify
    7.  * it under the terms of the GNU General Public License as published by
    8.  * the Free Software Foundation; either version 3 of the License, or
    9.  * (at your option) any later version.
    10.  *
    11.  * This program is distributed in the hope that it will be useful,
    12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    14.  * GNU General Public License for more details.
    15.  *
    16.  * You should have received a copy of the GNU General Public License
    17.  * along with this program; if not, see <http://www.gnu.org/licenses/>.
    18.  *
    19.  * Button Design, and Idea for the Layout are lean out from LillePOS, Copyright 2010, Martin Koller, [email]kollix@aon.at[/email]
    20.  *
    21. */
    22.  
    23. #ifndef DATABASEMANAGER_H
    24. #define DATABASEMANAGER_H
    25.  
    26. #include "qrkcore_global.h"
    27.  
    28. #include <QMutex>
    29. #include <QHash>
    30. #include <QSqlDatabase>
    31. #include <QMap>
    32.  
    33. class QThread;
    34.  
    35. class QRK_EXPORT DatabaseManager
    36. {
    37. public:
    38. static QSqlDatabase database(const QString& connectionName = QLatin1String(QSqlDatabase::defaultConnection));
    39. static void clear();
    40. static void removeCurrentThread(QString);
    41.  
    42. private:
    43. static QMutex s_databaseMutex;
    44. static QMap<QString, QMap<QString, QSqlDatabase>> s_instances;
    45.  
    46. };
    47.  
    48. #endif // DATABASEMANAGER_H
    To copy to clipboard, switch view to plain text mode 

    databasemanager.cpp

    Qt Code:
    1. /*
    2.  * This file is part of QRK - Qt Registrier Kasse
    3.  *
    4.  * Copyright (C) 2015-2018 Christian Kvasny <chris@ckvsoft.at>
    5.  *
    6.  * This program is free software; you can redistribute it and/or modify
    7.  * it under the terms of the GNU General Public License as published by
    8.  * the Free Software Foundation; either version 3 of the License, or
    9.  * (at your option) any later version.
    10.  *
    11.  * This program is distributed in the hope that it will be useful,
    12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    14.  * GNU General Public License for more details.
    15.  *
    16.  * You should have received a copy of the GNU General Public License
    17.  * along with this program; if not, see <http://www.gnu.org/licenses/>.
    18.  *
    19.  * Button Design, and Idea for the Layout are lean out from LillePOS, Copyright 2010, Martin Koller, [email]kollix@aon.at[/email]
    20.  *
    21. */
    22.  
    23. #include "databasemanager.h"
    24.  
    25. #include <QSqlDatabase>
    26. #include <QMutexLocker>
    27. #include <QThread>
    28. #include <QSqlError>
    29. #include <QJsonObject>
    30. #include <QDebug>
    31.  
    32. QMutex DatabaseManager::s_databaseMutex;
    33. QMap<QString, QMap<QString, QSqlDatabase> > DatabaseManager::s_instances;
    34.  
    35. QSqlDatabase DatabaseManager::database(const QString& connectionName)
    36. {
    37. QMutexLocker locker(&s_databaseMutex);
    38. QThread *thread = QThread::currentThread();
    39.  
    40. if (!thread->objectName().isEmpty()) {
    41. QString objectname = QString::number((long long)QThread::currentThread(), 16);
    42. // if we have a connection for this thread, return it
    43. QMap<QString, QMap<QString, QSqlDatabase> >::Iterator it_thread = s_instances.find(objectname);
    44. if (it_thread != s_instances.end()) {
    45. QMap<QString, QSqlDatabase>::iterator it_conn = it_thread.value().find(connectionName);
    46. if (it_conn != it_thread.value().end()) {
    47. QSqlDatabase connection = it_conn.value();
    48. qDebug() << "Function Name: " << Q_FUNC_INFO << " found SQL connection instances Thread: " << thread << " Name: " << connectionName;
    49. if (connection.isValid())
    50. return it_conn.value();
    51. }
    52. }
    53. }
    54.  
    55. QString objectname = QString::number((long long)QThread::currentThread(), 16);
    56.  
    57. thread->setObjectName(objectname);
    58. // otherwise, create a new connection for this thread
    59.  
    60. // cloneDatabase will not work with QT 5.11
    61. /* QSqlDatabase connection = QSqlDatabase::cloneDatabase(
    62.   QSqlDatabase::database(connectionName),
    63.   QString("%1_%2").arg(connectionName).arg(objectname));
    64.   */
    65.  
    66. QJsonObject connectionDefinition = Database::getConnectionDefinition();
    67. QString dbtype = connectionDefinition.value("dbtype").toString();
    68. QSqlDatabase connection = QSqlDatabase::addDatabase(dbtype, QString("%1_%2").arg(connectionName).arg(objectname));
    69.  
    70. if (dbtype == "QMYSQL") {
    71. connection.setHostName(connectionDefinition.value("databasehost").toString());
    72. connection.setUserName(connectionDefinition.value("databaseusername").toString());
    73. connection.setPassword(connectionDefinition.value("databasepassword").toString());
    74. connection.setConnectOptions(connectionDefinition.value("databaseoptions").toString());
    75. }
    76. connection.setDatabaseName(connectionDefinition.value("databasename").toString());
    77.  
    78. // open the database connection
    79. // initialize the database connection
    80. if (!connection.open()) {
    81. // Todo: Exeption Handling
    82. qCritical() << "Function Name: " << Q_FUNC_INFO << connection.lastError().text();
    83. return connection;
    84. }
    85.  
    86. qDebug() << "Function Name: " << Q_FUNC_INFO << " new SQL connection instances Thread: " << thread->currentThread() << " Name: " << connectionName;
    87.  
    88. s_instances[objectname][connectionName] = connection;
    89. qDebug() << "Function Name: " << Q_FUNC_INFO << " connection instances used: " << s_instances.size();
    90.  
    91. return connection;
    92. }
    93.  
    94. void DatabaseManager::clear()
    95. {
    96. s_instances.clear();
    97. }
    98.  
    99. void DatabaseManager::removeCurrentThread(QString connectionName)
    100. {
    101. QString objectname = QString::number((long long)QThread::currentThread(), 16);
    102. if (s_instances.contains(objectname)) {
    103. QMap<QString, QSqlDatabase> map = s_instances.value(objectname);
    104. QSqlDatabase connection = map.value(connectionName);
    105. connection.close();
    106. if (!connection.isOpen()) {
    107. s_instances.remove(objectname);
    108. qDebug() << "Function Name: " << Q_FUNC_INFO << " remove connection instance: " << objectname;
    109. }
    110. }
    111.  
    112. qDebug() << "Function Name: " << Q_FUNC_INFO << " connection instances used: " << s_instances.size();
    113. }
    To copy to clipboard, switch view to plain text mode 

    lg chris

  3. #3
    Join Date
    Jun 2018
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: requested database does not belong to the calling thread

    Close the connection to the database after each database operation or create one connection in the main thread

  4. #4
    Join Date
    Dec 2009
    Posts
    65
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: requested database does not belong to the calling thread

    This was not logical restriction and i created a bug report at https://bugreports.qt.io
    Bug is accepted and marked urgent/important. (QTBUG-72545), it was not inteded for cloneDatabase and it was as they say 'unwanted side effect'.
    I hope it gets fixed with 5.12.1

Similar Threads

  1. Database stored funtions calling
    By gbaguma in forum Qt Programming
    Replies: 8
    Last Post: 11th July 2013, 11:12
  2. QT thread classes quidance requested
    By bajarangi in forum Qt Programming
    Replies: 0
    Last Post: 26th May 2009, 14:56
  3. Can a Qwidget belong to two different layouts
    By ioannis in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2009, 11:29
  4. Calling one new thread with in existing thread?
    By ashukla in forum Qt Programming
    Replies: 1
    Last Post: 24th September 2007, 15:09
  5. Frozen? ... no ... calling 01800 Database
    By chaosgeorge in forum Qt Programming
    Replies: 2
    Last Post: 26th November 2006, 14:06

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.