Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

check if the sql connection is alive



  • 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



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



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



  • 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();
        }
    


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



  • @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 alive

    Umm, 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?



  • @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 alive

    Umm, 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



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


Log in to reply