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. Updating data from database in real time
QtWS25 Last Chance

Updating data from database in real time

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 889 Views
  • 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.
  • R Offline
    R Offline
    Rasul
    wrote on last edited by
    #1

    Guys hello!

    Im faced with the problem of updating in real time the data received from the database.
    The memory allocated for the request does not have time to be cleared. So there is a memory leak in my application.

    Im using Qt 5.15.2 and PostgreSql 15.1.

    There is a timer that calls the updating function every moment:

    QTimer *timer = new QTimer();
    connect(timer, SIGNAL(timeout()), this, SLOT(updateData()));
    timer->start();
    

    And there is the updating function:

    void MainWindow::updateData()
    {
        QSqlQuery *query;
        query = new QSqlQuery(db);
    
        if (query->exec("SELECT id FROM data"))
        {
            while (query->next())
            {
                /**/
            }
        }
    
        query->clear();
        query->finish();
    }
    

    Im using clear and finish functions to flush and to break the connection. But maybe i misunderstood smth?

    How can i recreate/update the request every moment?

    Are there ways to solve this problem?
    Maybe you have better solution?

    JonBJ Christian EhrlicherC 2 Replies Last reply
    0
    • R Rasul

      Guys hello!

      Im faced with the problem of updating in real time the data received from the database.
      The memory allocated for the request does not have time to be cleared. So there is a memory leak in my application.

      Im using Qt 5.15.2 and PostgreSql 15.1.

      There is a timer that calls the updating function every moment:

      QTimer *timer = new QTimer();
      connect(timer, SIGNAL(timeout()), this, SLOT(updateData()));
      timer->start();
      

      And there is the updating function:

      void MainWindow::updateData()
      {
          QSqlQuery *query;
          query = new QSqlQuery(db);
      
          if (query->exec("SELECT id FROM data"))
          {
              while (query->next())
              {
                  /**/
              }
          }
      
          query->clear();
          query->finish();
      }
      

      Im using clear and finish functions to flush and to break the connection. But maybe i misunderstood smth?

      How can i recreate/update the request every moment?

      Are there ways to solve this problem?
      Maybe you have better solution?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Rasul
      I don't know what exactly you think you re trying to achieve. I don't think clear() or flush() are needed. Your current code leaks a new QSqlQuery(db) every time. You could just use a stack variable:

          QSqlQuery query(db);
          if (query.exec("SELECT id FROM data"))
          {
              while (query.next())
              {
                  /**/
              }
          }
      

      You then run this on a timer every so often. Shame that you need to keep re-querying, but you know what the situation is.

      The memory allocated for the request does not have time to be cleared.

      Then you have a problem! You mustn't be issuing the query millions of times. Any resources allocated for the queries are probably released the next time you allow the Qt event loop to be entered, like between timeouts.

      1 Reply Last reply
      1
      • R Rasul

        Guys hello!

        Im faced with the problem of updating in real time the data received from the database.
        The memory allocated for the request does not have time to be cleared. So there is a memory leak in my application.

        Im using Qt 5.15.2 and PostgreSql 15.1.

        There is a timer that calls the updating function every moment:

        QTimer *timer = new QTimer();
        connect(timer, SIGNAL(timeout()), this, SLOT(updateData()));
        timer->start();
        

        And there is the updating function:

        void MainWindow::updateData()
        {
            QSqlQuery *query;
            query = new QSqlQuery(db);
        
            if (query->exec("SELECT id FROM data"))
            {
                while (query->next())
                {
                    /**/
                }
            }
        
            query->clear();
            query->finish();
        }
        

        Im using clear and finish functions to flush and to break the connection. But maybe i misunderstood smth?

        How can i recreate/update the request every moment?

        Are there ways to solve this problem?
        Maybe you have better solution?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Rasul said in Updating data from database in real time:

        The memory allocated for the request does not have time to be cleared. So there is a memory leak in my application.

        Because you nowhere delete your query - what do you expect... @JonB already told you what you can do to make it easier / do not have to care about the proper destruction of your objects.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rasul
          wrote on last edited by
          #4

          @JonB
          What im trying to achieve:
          Im working on a ticketing application.
          Data about booked and purchased tickets are displayed for each user on the seating arrangement.
          To avoid buying an already booked ticket by another user, I update the ticket data for each user every moment.

          Ive applied your solution in my program.
          It seems to have helped.
          Thanks for the reply!!

          In the end, to solve my problem, I should call update function less often and wait previous query to be cleared?

          Christian EhrlicherC JonBJ 2 Replies Last reply
          0
          • R Rasul

            @JonB
            What im trying to achieve:
            Im working on a ticketing application.
            Data about booked and purchased tickets are displayed for each user on the seating arrangement.
            To avoid buying an already booked ticket by another user, I update the ticket data for each user every moment.

            Ive applied your solution in my program.
            It seems to have helped.
            Thanks for the reply!!

            In the end, to solve my problem, I should call update function less often and wait previous query to be cleared?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Rasul said in Updating data from database in real time:

            To avoid buying an already booked ticket by another user, I update the ticket data for each user every moment.

            Why not use the database facilities for this - write your update statement so that it will fail when the ticket was already sold somewhere else.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3
            • R Offline
              R Offline
              Rasul
              wrote on last edited by
              #6

              @Christian-Ehrlicher

              Thanks for the help and advices!
              Im really appreciate that. I've been suffering with this problem lots of time XD

              Im displaying seating grid for every user.
              My updators mechanic:
              When a person books a seat, data about that seat is moved to the database;
              From the database, information about the seat is transmitted to each user (to avoid buying an already booked).

              Thats why im using this method.

              Thanks for your reply. Ill try to apply your solution too!

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Either use a trigger to inform the other clients or update once when you show it initially and then when they reserve. Everything else looks like overkill to me.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                2
                • R Rasul

                  @JonB
                  What im trying to achieve:
                  Im working on a ticketing application.
                  Data about booked and purchased tickets are displayed for each user on the seating arrangement.
                  To avoid buying an already booked ticket by another user, I update the ticket data for each user every moment.

                  Ive applied your solution in my program.
                  It seems to have helped.
                  Thanks for the reply!!

                  In the end, to solve my problem, I should call update function less often and wait previous query to be cleared?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Rasul said in Updating data from database in real time:

                  To avoid buying an already booked ticket by another user, I update the ticket data for each user every moment.

                  In the end, to solve my problem, I should call update function less often and wait previous query to be cleared?

                  If you want to be "realistic" frequently issuing queries to keep updated at the client is not the way to go. Just how often are you doing this timer query?

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Rasul
                    wrote on last edited by
                    #9

                    @JonB
                    Now I decided to update it every 10 ms. Its not to overload memory with millions requests. it's realistic enough in my opinion.
                    What do you think?

                    Thanks for your previous reply!! Your solution really worked for me!

                    Christian EhrlicherC JonBJ 2 Replies Last reply
                    0
                    • R Rasul

                      @JonB
                      Now I decided to update it every 10 ms. Its not to overload memory with millions requests. it's realistic enough in my opinion.
                      What do you think?

                      Thanks for your previous reply!! Your solution really worked for me!

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Rasul said in Updating data from database in real time:

                      Now I decided to update it every 10 ms.

                      This is just nonsense - 100Hz for something such useless... I would go with 1-5 seconds if at all as written in my previous comments.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        Rasul
                        wrote on last edited by
                        #11

                        @Christian-Ehrlicher
                        Oh. Ok. Ill stay on 1-5 seconds then. Thanks!

                        Yeah. I had taken notes of your previous comments.

                        1 Reply Last reply
                        0
                        • R Rasul

                          @JonB
                          Now I decided to update it every 10 ms. Its not to overload memory with millions requests. it's realistic enough in my opinion.
                          What do you think?

                          Thanks for your previous reply!! Your solution really worked for me!

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

                          @Rasul said in Updating data from database in real time:

                          What do you think?

                          Exactly as @Christian-Ehrlicher has written. No, you really don't want to be re-executing the SQL query 100 times per second! :)

                          1 Reply Last reply
                          1

                          • Login

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