Updating data from database in real time
-
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? -
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?@Rasul
I don't know what exactly you think you re trying to achieve. I don't thinkclear()
orflush()
are needed. Your current code leaks anew 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.
-
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?@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.
-
@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?
-
@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?
@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.
-
Thanks for the help and advices!
Im really appreciate that. I've been suffering with this problem lots of time XDIm 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!
-
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.
-
@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?
@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?
-
@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!
@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.
-
@Christian-Ehrlicher
Oh. Ok. Ill stay on 1-5 seconds then. Thanks!Yeah. I had taken notes of your previous comments.
-
@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!
@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! :)