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!
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!