0

In my project i have several Objects which gather some data and put them into a database, now for some reasons the database link could fail, so i made signal for it and bound it to a slot which will write what happened in a log file and try to reconnect to the database.

Now the problem is, sometimes it happens that a signal trigger the slot and then try to reconnect to the database while a previous connection is on his way. And as the Objects send several requests in a very small interval this problem happens always, because as soon as a request fails the signal is emitted to re-establish the connection.

So what I want is a proper way to prevent any slot to call the "connectToDatabase()" method when another slot already did it so that the method will end properly.

here is the slot.

void mainFrame::reconnectDatabase(QSqlQuery *failedQuery)
{ 
    log_it("QUERY:\""+failedQuery->lastQuery()+"\" ERROR:\""+failedQuery->lastError().text()+"\"");
    log_it("Re-connecting to Database...");
    QSqlDatabase db = QSqlDatabase::database();
    if(!db.open())
        log_it("Reconnecting database operation failed! REASON: \""+db.lastError().text()+"\"");
}

log_it() is my function for writing logs.

Xsmael
  • 3,624
  • 7
  • 44
  • 60

2 Answers2

2

You could accomplish this by having a boolean check indicating that an attempt to connect to the database is already in progress.

void MyClass::slotConnectToDatabase()
{
    if (m_connectingToDatabase)
        return;

    m_connectingToDatabase = true;
    // connect to database
    m_connectingToDatabase = false;
}
mbroadst
  • 768
  • 6
  • 8
  • this could work, but I'm using QSqlDatabase, and this class has no signals to say hey! i'm connected now!, to allow me to put the boolean to false – Xsmael May 06 '14 at 17:48
  • you would put this in the slot for the signal you created above. I'm assuming you're attempting to call QSqlDatabase::connect and when that fails you emit a signal, in this case then you'd attach to this slot. It would also be very helpful if you could provide some sample code to work with – mbroadst May 06 '14 at 17:51
  • I don't see why you can't add a boolean inside your reconnectDatabase method. You are executing a QSqlQuery and calling reconnectDatabase when that query fails right? So inside there, you just check to see if this method has already been called. Maybe you've omitted some more important code that could be included above? – mbroadst May 06 '14 at 18:11
  • ah ok I got it, I thought the connection function was like QTcpSocket's one, launching the connection operation asynchronously and the emit a signal when something happen(fail or success) but I didn't found any signal for QSqlDatabase Class, now I understand. Thanks! – Xsmael May 07 '14 at 09:55
1

When connectToDatabase is called for the first time, you can set a boolean variable to true which shows thatconnectToDatabase is called. In connectToDatabase method you can check for this variable and return if it is true :

void connectToDatabase()
{
    if(!connected)
         connected = true;
    else
         return;

    //...
}
Nejat
  • 31,784
  • 12
  • 106
  • 138