Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QtConcurrent to connect to db
Forum Updated to NodeBB v4.3 + New Features

QtConcurrent to connect to db

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
3 Posts 3 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • NickVN Offline
    NickVN Offline
    NickV
    wrote on last edited by kshegunov
    #1

    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]

    M 1 Reply Last reply
    0
    • H Offline
      H Offline
      hamer
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • NickVN NickV

        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]

        M Offline
        M Offline
        mjsurette
        wrote on last edited by
        #3

        @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.

        Mike

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved