check if the sql connection is alive
-
wrote on 2 Oct 2021, 19:39 last edited by
Hi,
How can i check if the connection to the database is alive? after the connection is established the isOpen() will always return true even if the connection is dropped(i.e the database server is down or unreachable)
so how can i check if the connection is really alive and can accept queries?regards
-
Hi,
How can i check if the connection to the database is alive? after the connection is established the isOpen() will always return true even if the connection is dropped(i.e the database server is down or unreachable)
so how can i check if the connection is really alive and can accept queries?regards
wrote on 2 Oct 2021, 20:35 last edited by@rhx9 said in check if the sql connection is alive:
so how can i check if the connection is really alive and can accept queries?
Send it a query.
-
wrote on 2 Oct 2021, 22:33 last edited by
First try:
if(!db.open()){ qDebug() << "SQL Database not Connected"; } else { qDebug() << "SQL Database is Connected"; }
Than make your query and try:
if(!query.isActive()){ qWarning() << "ERROR: " << query.lastError().text(); }
-
@rhx9 said in check if the sql connection is alive:
so how can i check if the connection is really alive and can accept queries?
Send it a query.
wrote on 3 Oct 2021, 09:38 last edited by@JonB said in check if the sql connection is alive:
@rhx9 said in check if the sql connection is alive:
so how can i check if the connection is really alive and can accept queries?
Send it a query.
That's what i eventually did, ( I sent this query
SELECT 1
every 4 seconds to check if the database is alive)
Thanks -
@JonB said in check if the sql connection is alive:
@rhx9 said in check if the sql connection is alive:
so how can i check if the connection is really alive and can accept queries?
Send it a query.
That's what i eventually did, ( I sent this query
SELECT 1
every 4 seconds to check if the database is alive)
Thankswrote on 3 Oct 2021, 09:56 last edited by@rhx9
Yep, that's the only sure way to determine...I sent this query
SELECT 1
every 4 seconds to check if the database is aliveUmm, yes, but why? This keeps both your app and the database (e.g. if it's a SQL server) busy. You're not really supposed to do that. What is the point of this, you are just supposed to send the queries you actually want when you want them, I can't see what your regular query achieves?
-
@rhx9
Yep, that's the only sure way to determine...I sent this query
SELECT 1
every 4 seconds to check if the database is aliveUmm, yes, but why? This keeps both your app and the database (e.g. if it's a SQL server) busy. You're not really supposed to do that. What is the point of this, you are just supposed to send the queries you actually want when you want them, I can't see what your regular query achieves?
wrote on 3 Oct 2021, 10:28 last edited by@JonB said in check if the sql connection is alive:
@rhx9
Yep, that's the only sure way to determine...I sent this query
SELECT 1
every 4 seconds to check if the database is aliveUmm, yes, but why? This keeps both your app and the database (e.g. if it's a SQL server) busy. You're not really supposed to do that. What is the point of this, you are just supposed to send the queries you actually want when you want them, I can't see what your regular query achieves?
I do this to check if the sql connection is alive, if it is alive and can accept queries a signal will be emitted that will show an icon in the status bar that indicates the db connection is ok, if the connection is not ok an icon that shows the connection is not ok will be shown.
Regards
-
@JonB said in check if the sql connection is alive:
@rhx9
Yep, that's the only sure way to determine...I sent this query
SELECT 1
every 4 seconds to check if the database is aliveUmm, yes, but why? This keeps both your app and the database (e.g. if it's a SQL server) busy. You're not really supposed to do that. What is the point of this, you are just supposed to send the queries you actually want when you want them, I can't see what your regular query achieves?
I do this to check if the sql connection is alive, if it is alive and can accept queries a signal will be emitted that will show an icon in the status bar that indicates the db connection is ok, if the connection is not ok an icon that shows the connection is not ok will be shown.
Regards
wrote on 3 Oct 2021, 10:44 last edited by@rhx9
Well like I said you're not really supposed to do that. For example, if this were on a mobile/laptop neither the client nor the server (if remote) machine will ever get to sleep/hibernate. Even on a desktop if I run up your program and walk away for two days it will still keep both ends busy. Your client is supposed to be (sort of) "stateless" like an HTTP connection, we don't (or shouldn't) have a web page constantly polling the server.Up to you.
1/7