Hi all,

Im having trouble with QSqlDatabase::addDatabase. I have a PostgresDriver class which I use for connecting to DB and making some queries. I can create any of these in the main cpp file. However I also have an HTTPServer which depending on the request, also creates a PostgresDriver to . This is where the QSqlDatabase::addDatabase() (and for what I can see in the stack trace, in its inner moveToThread) crashes.

I've read all the posts and documentation saying that connections are not thread safe and that should not be reused in other threads. I also presume that the HTTPSERVER will handle the requests spreaded in different threads and therefore the place where I am creating the new PostgresDriver is no the main thread. But for what I understood there should not be any problem if what I am doing is creating a new connection (not reusing one from another thread).


Qt Code:
  1. ///////////////////////////////////////////////////////// POSTGRESDRIVER.H
  2.  
  3. class PostgresDriver : public QObject
  4. {
  5. public:
  6. PostgresDriver(QString url, int port, QString db_name, QString db_user, QString db_pass, QString connection_name = "shared_connection");
  7. ~PostgresDriver();
  8. QList<QSqlRecord> executeQuery(QString sql);
  9.  
  10. private:
  11. QSqlDatabase database;
  12. QString connection_name;
  13. };
  14.  
  15. ///////////////////////////////////////////////////////// POSTGRESDRIVER.CPP
  16.  
  17. #include "PostgresDriver.h"
  18.  
  19. PostgresDriver::PostgresDriver(QString url, int port, QString db_name, QString db_user, QString db_pass, QString connection_name) : QObject()
  20. {
  21. this->connection_name = connection_name;
  22.  
  23. this->database = QSqlDatabase::addDatabase("QPSQL" , this->connection_name); // HERE IS WHERE IT BREAKS IF DRIVER IS CREATED NOT IN THE MAIN THREAD
  24. this->database.setHostName( url );
  25. this->database.setPort( port );
  26. this->database.setDatabaseName( db_name );
  27. this->database.setUserName( db_user );
  28. this->database.setPassword( db_pass );
  29.  
  30. while( !this->database.open() ){
  31. this->thread()->sleep( 2 ); // Wait 2 seconds
  32. qWarning() << "[PostgresDriver::connectDB] Unable to connect to DB : " << this->database.hostName() << this->database.port() << this->database.databaseName() << " waiting..." << endl;
  33. }
  34. }
  35.  
  36. PostgresDriver::~PostgresDriver(){
  37. this->database.close();
  38. this->database.removeDatabase( this->connection_name );
  39. }
  40.  
  41. QList<QSqlRecord> PostgresDriver::executeQuery( QString sql ){
  42.  
  43. QList<QSqlRecord> list;
  44.  
  45. if( this->database.isOpen() ){
  46. QSqlQuery query( this->database.database( this->connection_name ) );
  47.  
  48. query.exec( sql );
  49.  
  50. QSqlError error = query.lastError();
  51. if (error.type() != QSqlError::NoError){
  52. qWarning() << error.text() << endl << sql << endl;
  53. }
  54.  
  55. while( query.next() ){
  56. list.append(query.record());
  57. }
  58.  
  59. } else {
  60. qWarning() << "[PostgresDriver::executeQuery] Could not execute query because database is closed." << endl;
  61. }
  62.  
  63. return list;
  64. }
To copy to clipboard, switch view to plain text mode 

STACK TRACE:
0 QObject::moveToThread(QThread *) 0x7fb1885167a7
1 QFactoryLoader::instance(int) const 0x7fb1884c9df5
2 ?? 0x7fb188d42c74
3 QSqlDatabase::addDatabase(QString const&, QString const&) 0x7fb188d431f1
4 PostgresDriver::PostgresDriver PostgresDriver.cpp 12 0x4233aa // MY POSTGRESDRIVER CLASS
5 RequestHandler::HTTP_Create RequestHandler.cpp 175 0x41667e // MY HTTPSERVER REQUEST HANDLER



I've even tried creating a centralized DBController which was created in the main thread and dispatched connections to other threads but same thing happened. Any clues about this?

Thanks in advance