Uploaded image for project: 'MariaDB Connector/C'
  1. MariaDB Connector/C
  2. CONC-589

First query after connection was lost fails when auto-reconnect enabled

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • None
    • None
    • Other
    • None

    Description

      The reconnect feature that can be enabled by setting MYSQL_OPT_RECONNECT to true seems to always fail the first query after the connection was lost with the error "Lost connection to server during query".
      The second query that runs after the connection succeeds and the connection is re-established.

      The expected behaviour (which is also the behaviour of libmysql) would be for the connection to be re-established without any query failing.

      The MySQL documentation specifically mentions that any queries that are sent after the connection was lost will be retried if auto-reconnect is enabled but I could not find any documentation like this for the MariaDB C Connector.

      Here is a small piece of code that can be used to replicate the issue. It requires the MySQL/MariaDB server to be restarted within the 10 seconds waiting period to demonstrate the issue.

      #include <iostream>
      #include <thread>
      #include <chrono>
       
      #include <mysql/mysql.h>
       
      using namespace std::chrono_literals;
       
      void runQuery(MYSQL *sql) {
          std::string queryStr = "SELECT 1";
          MYSQL_RES *resultSet;
          if (mysql_real_query(sql, queryStr.c_str(), queryStr.size()) ||
              (resultSet = mysql_store_result(sql)) == nullptr) {
              std::cout << "Query failed with error: " << mysql_error(sql) << std::endl;
          } else {
              mysql_free_result(resultSet);
              std::cout << "Query finished successfully" << std::endl;
          }
      }
       
      int main() {
          my_bool myAutoReconnectBool = '1';
       
          auto sql = mysql_init(nullptr);
          mysql_options(sql, MYSQL_OPT_RECONNECT, &myAutoReconnectBool);
          if (!mysql_real_connect(sql, "127.0.0.1", "root", "", "mysql", 3306, nullptr, 0)) {
              std::cout << mysql_error(sql) << std::endl;
              exit(1);
          }
          std::cout << std::endl << "Connected successfully" << std::endl;
       
          std::cout << std::endl << "Running first query" << std::endl;
          runQuery(sql);
       
          //Restart your mysql/mariadb server after the first query finished successfully
          std::cout << std::endl << "Waiting 10 seconds..." << std::endl;
          std::this_thread::sleep_for(10s);
       
          std::cout << std::endl << "Running second query" << std::endl;
          runQuery(sql);
       
          std::cout << std::endl << "Running third query" << std::endl;
          runQuery(sql);
       
          mysql_close(sql);
          return 0;
      }
      

      Output using libmysql:

      Connected successfully
       
      Running first query
      Query finished successfully
       
      Waiting 10 seconds...
       
      Running second query
      Query finished successfully
       
      Running third query
      Query finished successfully
      
      

      Output using libmariadb:

      Connected successfully
       
      Running first query
      Query finished successfully
       
      Waiting 10 seconds...
       
      Running second query
      Query failed with error: Lost connection to server during query
       
      Running third query
      Query finished successfully
      

      Attachments

        Activity

          People

            georg Georg Richter
            FredyH Frederik Haselmeier
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.