PDA

View Full Version : Behavior of MYSQL_OPT_RECONNECT seems misleading



Urthas
18th August 2015, 19:46
Hello,

The MySQL documentation for Controlling Automatic Reconnection Behavior (http://dev.mysql.com/doc/refman/5.7/en/auto-reconnect.html) states that:


The MySQL client library can perform an automatic reconnection to the server if it finds that the connection is down when you attempt to send a statement to the server to be executed. If auto-reconnect is enabled, the library tries once to reconnect to the server and send the statement again.

However, consider the following code:


// class PhoneSystemDB is-a QSqlDatabase whose ctor specifies the QMYSQL driver

bool PhoneSystemDB::initialize() {
if (isValid()) {
setConnectOptions("MYSQL_OPT_RECONNECT=1");
setHostName("localhost");
setUserName("calvin");
setDatabaseName("hobbes");
setPort(0);
}
return !lastError().isValid();
}

// class TestPhoneSystemDB

void TestPhoneSystemDB::initTestCase() {
QVERIFY(dbConnection.initialize());
QVERIFY(dbConnection.open());
}

void TestPhoneSystemDB::testReconnect() {
const QString options = dbConnection.connectOptions();
QVERIFY(options.contains("MYSQL_OPT_RECONNECT") && !options.contains("MYSQL_OPT_RECONNECT=0"));
dbConnection.close();
QVERIFY(dbConnection.exec("SELECT VERSION()").isValid());
}

And the test results:


********* Start testing of TestPhoneSystemDB *********
Config: Using QtTest library 5.5.0, Qt 5.5.0 (x86_64-little_endian-lp64 shared (dynamic) release build; by Clang 6.0 (clang-600.0.56) (Apple))
PASS : TestPhoneSystemDB::initTestCase()
QWARN : TestPhoneSystemDB::testReconnect() QSqlQuery::exec: database not open
FAIL! : TestPhoneSystemDB::testReconnect() 'query.isValid()' returned FALSE. ()
Loc: [../test/testphonesystemdb.cpp(13)]
PASS : TestPhoneSystemDB::cleanupTestCase()
Totals: 2 passed, 1 failed, 0 skipped, 0 blacklisted
********* Finished testing of TestPhoneSystemDB *********

Did I misunderstand the meaning of "the connection is down" in the documentation? What is the correct interpretation? Alternatively, did I correctly interpret the documentation but the attempt to reconnect is simply failing? Perhaps the better question is how might I test the reconnect behavior of the database? Any insight is appreciated.

Thank you!

Urthas
18th August 2015, 23:03
Update: if I alter the testReconnect() code as follows (not much of a test, admittedly),


void TestPhoneSystemDB::testReconnect() {
const QString options = dbConnection.connectOptions();
QVERIFY(options.contains("MYSQL_OPT_RECONNECT") && !options.contains("MYSQL_OPT_RECONNECT=0"));
QVERIFY(dbConnection.isValid());
QVERIFY(dbConnection.isOpen());
dbConnection.exec("SELECT VERSION()");
qDebug() << "Got error:" << dbConnection.lastError().text();
}

then the test passes but I get the following error message:


QDEBUG : TestPhoneSystemDB::testReconnect() Got error: "MySQL server has gone away QMYSQL: Unable to execute query"

I guess something is wrong with my database setup...?

Update: if I alter the testReconnect() code as follows (not much of a test, admittedly),


void TestPhoneSystemDB::testReconnect() {
const QString options = dbConnection.connectOptions();
QVERIFY(options.contains("MYSQL_OPT_RECONNECT") && !options.contains("MYSQL_OPT_RECONNECT=0"));
QVERIFY(dbConnection.isValid());
QVERIFY(dbConnection.isOpen());
dbConnection.exec("SELECT VERSION()");
qDebug() << "Got error:" << dbConnection.lastError().text();
}

then the test passes but I get the following error message:


QDEBUG : TestPhoneSystemDB::testReconnect() Got error: "MySQL server has gone away QMYSQL: Unable to execute query"

I guess something is wrong with my database setup...?

Added after 44 minutes:

OK, so I feel pretty stupid. It helps to actually create the database and user -- and assign the user the appropriate privileges -- before trying to use QSqlDatabase to do anything useful. :rolleyes: