how to reconnect CORRECTLY qmysql database?
Hi all,
I've installed qt-sdk-win-opensource-2009.05.exe and then wrote a simple program to test whether QMYSQL plugin was successfully built and connected. As of now there is no problem but I need mysql_ping functionality. How to try to reconnect correctly if isOpen function returns false?
I've attached a part of code. Before executing a query, I want to test connectivity and reconnect if it's broken. In order to accomplish this, must removeDatabase be firstly called? Without definition of another a QSqlDatabase instance, can existing qtafx_db variable be usable? I'll pleasure so much if you can clarify this matter.
Wish you all the best,
Code:
int main (int argc, char** argv)
{
bool qStatus;
{
qtafx_db.setHostName ("");
qtafx_db.setDatabaseName ("");
qtafx_db.setUserName ("");
qtafx_db.setPassword ("");
qStatus = qtafx_db.open();
....
// before executing a query, I want to test connectivity and reconnect if it's broken
....
qtafx_db.close ();
qtafx_db.removeDatabase ();
}
}
Re: how to reconnect CORRECTLY qmysql database?
You could try enclosing the query in an if block:
Code:
if (!qtafx_db.open()){
// DB not open - create connection
}else
{
// Submit query
// and inspect QSqlQuery::lastError() to ensure it worked.
}
Re: how to reconnect CORRECTLY qmysql database?
You can try this:
Code:
int retries = 5;
int delay = 1000 * 3; // time in ms
while (!qtafx_db.open() || retries)
{
delayTimer.start(delay);
loop.exec();
retries--;
}
if (!qtafx_db.isOpen())
{
qtafx_db.close ();
qtafx_db.removeDatabase ();
return 1; // Or your prefered code here or popup message
}
In this code sample you 5 times retries to connect to the database with 3 seconds delay between retries. Hope that helps.
Re: how to reconnect CORRECTLY qmysql database?
Small point but you are closing a connection that is not open in line 14.
Out of interest why would you want to poll a connection like this?
If you open a connection, you may as well try to action the query.
Test the lasterror value to ensure that the query executed properly.
You may wish to use transactions to guarantee that the whole query was executed if your DB supports them.
If the connection is so intermittent that it needs to be tested before each query, there is no gaurantee that the connection will still be there immediately after the test has completed.
Re: how to reconnect CORRECTLY qmysql database?
If you mean my code snippet then please take into consideration that it shows a piece of code. I don't know the parameters
yaseminyilmaz will use to open database. This is what's going on after you establish database connection.
Re: how to reconnect CORRECTLY qmysql database?
No, I was wondeing about the general approach of opening a connection and testing it before submitting any queries (that may still fail regardless), not your answer.
Re: how to reconnect CORRECTLY qmysql database?
I use the typical connection.h:
Code:
#ifndef CONNECTION_H
#define CONNECTION_H
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
db.setHostName(newHostName);
db.setDatabaseName(newDatabase);
db.setUserName(newUserName);
db.setPassword(newPassword);
if (!db.open()) {
qApp->trUtf8("Error message\n"
""
return false;
}
return true;
}
#endif
One question about this.
Every time i reconnect i get a
Code:
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
Is there a problem with this?
Re: how to reconnect CORRECTLY qmysql database?
It sounds as though you are trying to open multiple connections to your database without closing the previous ones.
It is normal to reuse the already open connection rather than open a new one each time you want to access your database.
In your case try calling the createConnection() just once at the start of the programme and submit your all queries using this.
If you need multiple connections for some reason give them different names.