Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlQuery::exec() freezes over OpenVPN
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery::exec() freezes over OpenVPN

Scheduled Pinned Locked Moved Unsolved General and Desktop
28 Posts 6 Posters 4.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.
  • J JonB
    8 Oct 2019, 13:07

    @petar
    The Qt SQL stuff uses drivers to do the low-level stuff. It may not be a "fault" in QSqlQuery::exec(), it may be the underlying protocol talking to SQL over OpenVPN which is what "freezes", and there is nothing the Qt level can do about that.

    P Offline
    P Offline
    petar
    wrote on 8 Oct 2019, 16:57 last edited by
    #11

    @JonB
    Aren't those drivers a part of Qt (eg. QPSQL for Postgres), it's not external driver.

    J J 2 Replies Last reply 8 Oct 2019, 17:05
    0
    • P petar
      8 Oct 2019, 16:57

      @JonB
      Aren't those drivers a part of Qt (eg. QPSQL for Postgres), it's not external driver.

      J Offline
      J Offline
      JonB
      wrote on 8 Oct 2019, 17:05 last edited by
      #12

      @petar
      Yes, but ultimately they must leverage some lower-level protocol code (e.g. ODBC, or whatever for MySQL or Postgres), and that is where it may be "freezing" over OpenVPN? I was just saying it may not be QSqlQuery::exec() code itself, which may be built atop other levels, which freezes, and it may not be able to do anything about it, which you are asking it to do.

      1 Reply Last reply
      0
      • P petar
        7 Oct 2019, 21:39

        Ok, and then what. Suppose the query in another thread succeeds. How can I use that query in main thread and attach it to a QSqlQueryModel model and display data in QTableView?

        If the query freezes in another thread, I suppose I can handle that with some timeout. Is there a way to kill the thread that left frozen?

        A Offline
        A Offline
        A.A.SEZEN
        wrote on 8 Oct 2019, 17:08 last edited by
        #13

        @petar said in QSqlQuery::exec() freezes over OpenVPN:

        Ok, and then what. Suppose the query in another thread succeeds. How can I use that query in main thread and attach it to a QSqlQueryModel model and display data in QTableView?
        If the query freezes in another thread, I suppose I can handle that with some timeout. Is there a way to kill the thread that left frozen?

        Yes.

        void MainWindow::threadStart()
        {
            threadQuery * tQuery = new threadQuery;
            connect(tQuery, &threadQuery::record, this, &MainWindow::record);
            tQuery->start();
        }
        
        void MainWindow::record(QSqlQueryModel *model)
        {
            ui->tableView->setModel(model);
        }
        
        void threadQuery::run()
        {
            QSqlQuery q(conn);
            if(q.exec("SELECT * FROM table")) {
                model->setQuery(q);
                emit this->record(model);
            }
        
        }
        
        
        P J 2 Replies Last reply 8 Oct 2019, 17:49
        0
        • A A.A.SEZEN
          8 Oct 2019, 17:08

          @petar said in QSqlQuery::exec() freezes over OpenVPN:

          Ok, and then what. Suppose the query in another thread succeeds. How can I use that query in main thread and attach it to a QSqlQueryModel model and display data in QTableView?
          If the query freezes in another thread, I suppose I can handle that with some timeout. Is there a way to kill the thread that left frozen?

          Yes.

          void MainWindow::threadStart()
          {
              threadQuery * tQuery = new threadQuery;
              connect(tQuery, &threadQuery::record, this, &MainWindow::record);
              tQuery->start();
          }
          
          void MainWindow::record(QSqlQueryModel *model)
          {
              ui->tableView->setModel(model);
          }
          
          void threadQuery::run()
          {
              QSqlQuery q(conn);
              if(q.exec("SELECT * FROM table")) {
                  model->setQuery(q);
                  emit this->record(model);
              }
          
          }
          
          
          P Offline
          P Offline
          petar
          wrote on 8 Oct 2019, 17:49 last edited by
          #14

          @A-A-SEZEN
          Ok, thanks, but what about this limit from the documentation?

          It says: "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."

          How can I further work with this query/model in my main thread?

          What happens when your code in thread:

          if(q.exec("SELECT * FROM table")) {
          

          (precisely exec() function) freezes?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            A.A.SEZEN
            wrote on 8 Oct 2019, 18:16 last edited by
            #15

            @petar
            Your application will not freeze due to Thread.
            You know the integrity of your code.

            P 1 Reply Last reply 8 Oct 2019, 19:15
            0
            • A A.A.SEZEN
              8 Oct 2019, 17:08

              @petar said in QSqlQuery::exec() freezes over OpenVPN:

              Ok, and then what. Suppose the query in another thread succeeds. How can I use that query in main thread and attach it to a QSqlQueryModel model and display data in QTableView?
              If the query freezes in another thread, I suppose I can handle that with some timeout. Is there a way to kill the thread that left frozen?

              Yes.

              void MainWindow::threadStart()
              {
                  threadQuery * tQuery = new threadQuery;
                  connect(tQuery, &threadQuery::record, this, &MainWindow::record);
                  tQuery->start();
              }
              
              void MainWindow::record(QSqlQueryModel *model)
              {
                  ui->tableView->setModel(model);
              }
              
              void threadQuery::run()
              {
                  QSqlQuery q(conn);
                  if(q.exec("SELECT * FROM table")) {
                      model->setQuery(q);
                      emit this->record(model);
                  }
              
              }
              
              
              J Offline
              J Offline
              JonB
              wrote on 8 Oct 2019, 18:39 last edited by
              #16

              @A-A-SEZEN

                  QSqlQuery q(conn);
                  if(q.exec("SELECT * FROM table")) {
                      model->setQuery(q);
              

              I don't know, but have you verified whether this does not execute the query twice?

              P 1 Reply Last reply 8 Oct 2019, 18:59
              0
              • J JonB
                8 Oct 2019, 18:39

                @A-A-SEZEN

                    QSqlQuery q(conn);
                    if(q.exec("SELECT * FROM table")) {
                        model->setQuery(q);
                

                I don't know, but have you verified whether this does not execute the query twice?

                P Offline
                P Offline
                petar
                wrote on 8 Oct 2019, 18:59 last edited by
                #17

                @JonB
                No, only once.

                1 Reply Last reply
                1
                • A A.A.SEZEN
                  8 Oct 2019, 18:16

                  @petar
                  Your application will not freeze due to Thread.
                  You know the integrity of your code.

                  P Offline
                  P Offline
                  petar
                  wrote on 8 Oct 2019, 19:15 last edited by
                  #18

                  @A-A-SEZEN
                  Can I use thread for queries working with QSqlQuery and QSqlQueryModel? I am not sure. What about sharing connections (that is not allowed) between threads, and sharing queries?

                  A 1 Reply Last reply 10 Oct 2019, 05:04
                  0
                  • P petar
                    8 Oct 2019, 16:57

                    @JonB
                    Aren't those drivers a part of Qt (eg. QPSQL for Postgres), it's not external driver.

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 9 Oct 2019, 04:48 last edited by
                    #19

                    @petar said in QSqlQuery::exec() freezes over OpenVPN:

                    Aren't those drivers a part of Qt (eg. QPSQL for Postgres), it's not external driver.

                    Those use client libraries provided by the SQL database system like libmysqlclient for MySQL. To see whether this is an issue in Qt or database system you can try to execute exactly the same query over VPN with the client application of your database system.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    P 1 Reply Last reply 9 Oct 2019, 17:30
                    2
                    • J jsulm
                      9 Oct 2019, 04:48

                      @petar said in QSqlQuery::exec() freezes over OpenVPN:

                      Aren't those drivers a part of Qt (eg. QPSQL for Postgres), it's not external driver.

                      Those use client libraries provided by the SQL database system like libmysqlclient for MySQL. To see whether this is an issue in Qt or database system you can try to execute exactly the same query over VPN with the client application of your database system.

                      P Offline
                      P Offline
                      petar
                      wrote on 9 Oct 2019, 17:30 last edited by petar 10 Sept 2019, 17:31
                      #20

                      @jsulm
                      Good point. I checked the same query over OpenVPN with LIMIT 25,000 - 200,000 records in Qt and in plain psql. In Qt QSqlQuery::exec() almost always freezes, while psql never.

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        petar
                        wrote on 9 Oct 2019, 21:07 last edited by
                        #21

                        I tested it and it turns out it freezes even in non GUI applications, so it doesn't have to do anything with the GUI thing.

                        I just started QSqlQuery::exec() in non GUI app and it freezes forever over OpenVPN, while the same code works ok over LAN or on localhost. On LAN sometimes QSqlQuery::exec() returns false which is ok, but never freezes forever.

                        J 1 Reply Last reply 10 Oct 2019, 05:18
                        0
                        • P petar
                          8 Oct 2019, 19:15

                          @A-A-SEZEN
                          Can I use thread for queries working with QSqlQuery and QSqlQueryModel? I am not sure. What about sharing connections (that is not allowed) between threads, and sharing queries?

                          A Offline
                          A Offline
                          A.A.SEZEN
                          wrote on 10 Oct 2019, 05:04 last edited by
                          #22

                          @petar said in QSqlQuery::exec() freezes over OpenVPN:

                          Can I use thread for queries working with QSqlQuery and QSqlQueryModel? I am not sure. What about sharing connections (that is not allowed) between threads, and sharing queries?

                          The codes above are thread codes I've tried for you.
                          I've already shared critical parts. The rest are standard thread class and database connections. It is up to you how much you can use within the integrity of your code.
                          SQLite does not allow concurrent operations to the same table. Other than that, you will have no problems. You can create a new SQLite connection in a thread, or you can include your connection class in a thread class. I have used both cases. I like to manage it myself instead of the model. I am using QTableWidget instead of QTableView. So I can't comment much on your situation.
                          Regards.

                          P 1 Reply Last reply 10 Oct 2019, 07:55
                          0
                          • P petar
                            9 Oct 2019, 21:07

                            I tested it and it turns out it freezes even in non GUI applications, so it doesn't have to do anything with the GUI thing.

                            I just started QSqlQuery::exec() in non GUI app and it freezes forever over OpenVPN, while the same code works ok over LAN or on localhost. On LAN sometimes QSqlQuery::exec() returns false which is ok, but never freezes forever.

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 10 Oct 2019, 05:18 last edited by
                            #23

                            @petar You can check Qt bug tracker and file a bug if there is nothing yet for this issue

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            P 1 Reply Last reply 10 Oct 2019, 07:46
                            0
                            • J jsulm
                              10 Oct 2019, 05:18

                              @petar You can check Qt bug tracker and file a bug if there is nothing yet for this issue

                              P Offline
                              P Offline
                              petar
                              wrote on 10 Oct 2019, 07:46 last edited by
                              #24

                              @jsulm
                              I've already done this but got the answer from Christian Ehrlicher that this is probably a bug inside the libpq Postgres lib and that there is nothing they can do about it (concerning Qt).

                              J 1 Reply Last reply 10 Oct 2019, 07:48
                              0
                              • P petar
                                10 Oct 2019, 07:46

                                @jsulm
                                I've already done this but got the answer from Christian Ehrlicher that this is probably a bug inside the libpq Postgres lib and that there is nothing they can do about it (concerning Qt).

                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 10 Oct 2019, 07:48 last edited by
                                #25

                                @petar You can comment in the bug and write that you tested without Qt and it worked.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                P 1 Reply Last reply 10 Oct 2019, 08:10
                                0
                                • A A.A.SEZEN
                                  10 Oct 2019, 05:04

                                  @petar said in QSqlQuery::exec() freezes over OpenVPN:

                                  Can I use thread for queries working with QSqlQuery and QSqlQueryModel? I am not sure. What about sharing connections (that is not allowed) between threads, and sharing queries?

                                  The codes above are thread codes I've tried for you.
                                  I've already shared critical parts. The rest are standard thread class and database connections. It is up to you how much you can use within the integrity of your code.
                                  SQLite does not allow concurrent operations to the same table. Other than that, you will have no problems. You can create a new SQLite connection in a thread, or you can include your connection class in a thread class. I have used both cases. I like to manage it myself instead of the model. I am using QTableWidget instead of QTableView. So I can't comment much on your situation.
                                  Regards.

                                  P Offline
                                  P Offline
                                  petar
                                  wrote on 10 Oct 2019, 07:55 last edited by
                                  #26

                                  @A-A-SEZEN
                                  Thanks for your reply. Interesting approach.

                                  1 Reply Last reply
                                  0
                                  • J jsulm
                                    10 Oct 2019, 07:48

                                    @petar You can comment in the bug and write that you tested without Qt and it worked.

                                    P Offline
                                    P Offline
                                    petar
                                    wrote on 10 Oct 2019, 08:10 last edited by
                                    #27

                                    @jsulm
                                    Thanks, I just did that, but Christian is a tough guy, hehe.

                                    1 Reply Last reply
                                    0
                                    • I Offline
                                      I Offline
                                      imahgin
                                      wrote on 11 Nov 2020, 10:43 last edited by
                                      #28

                                      Although it's been a while, here is my solution.
                                      I experienced a similar issue when connection was lost while a query was running.
                                      For PostgreSQL, there is an option to set a timeout in TCP connection: tcp_user_timeout

                                      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