Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. check if the sql connection is alive
Forum Updated to NodeBB v4.3 + New Features

check if the sql connection is alive

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 3 Posters 2.9k 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
    rhx9
    wrote on 2 Oct 2021, 19:39 last edited by
    #1

    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

    J 1 Reply Last reply 2 Oct 2021, 20:35
    0
    • R rhx9
      2 Oct 2021, 19:39

      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

      J Offline
      J Offline
      JonB
      wrote on 2 Oct 2021, 20:35 last edited by
      #2

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

      R 1 Reply Last reply 3 Oct 2021, 09:38
      0
      • B Offline
        B Offline
        BambusFan
        wrote on 2 Oct 2021, 22:33 last edited by
        #3

        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();
            }
        
        1 Reply Last reply
        0
        • J JonB
          2 Oct 2021, 20:35

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

          R Offline
          R Offline
          rhx9
          wrote on 3 Oct 2021, 09:38 last edited by
          #4

          @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

          J 1 Reply Last reply 3 Oct 2021, 09:56
          0
          • R rhx9
            3 Oct 2021, 09:38

            @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

            J Offline
            J Offline
            JonB
            wrote on 3 Oct 2021, 09:56 last edited by
            #5

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

            R 1 Reply Last reply 3 Oct 2021, 10:28
            0
            • J JonB
              3 Oct 2021, 09:56

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

              R Offline
              R Offline
              rhx9
              wrote on 3 Oct 2021, 10:28 last edited by
              #6

              @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

              J 1 Reply Last reply 3 Oct 2021, 10:44
              0
              • R rhx9
                3 Oct 2021, 10:28

                @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

                J Offline
                J Offline
                JonB
                wrote on 3 Oct 2021, 10:44 last edited by
                #7

                @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 Reply Last reply
                2

                1/7

                2 Oct 2021, 19:39

                • Login

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