QtConcurrent to connect to db

  • Hi. I would like to ask something:
    I'm trying to connect to database via qtconcurrent. I'm trying to do this because i want an asynchronous database connection in order not to have problem with my interface. I have a class named databasemanager. In the constructor of the class i run the addDatabase because i read somewhere that "A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported.". So in my main gui thread i create the database connection with addDatabase. With qt concurrent i run a function to open the database that i've already opened. Since here everything is ok!! Now i'm trying to call another function with qtconcurrent that executes another query in the db. But here i have a problem. Sometimes the query runs ok but in most of the times the code stop running in the exec function of the query and nothing happens after that. Any suggestions? I believe that the problem is that i am trying to use the database instance in another thread but is there any other way to do that.
    P.S. i don't want to clone the database connection and open it again because i use triggers in my database in order to notify me for any changes in the tables.

    [Related to QSqlquery exec and prepare sometimes stuck ~kshegunov]

  • You have to make sure that only one thread accesses the connection at the same time.

  • @NickV
    You may also be interested in this thread https://forum.qt.io/topic/71196/qsqltablemodel-submit-select-in-qthread-separated

    I'm not sure how you're using triggers to notify you of changes. The Postgresql database driver supports notification, a simple and reliable way to detect changes. You can find out if your database does by calling the driver's hasFeature(QSqlDriver::EventNotifications) method.


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.