PDA

View Full Version : qsqldatabase does not sense db disconnect



rburge
9th March 2006, 19:59
I use the following code to connect to an oracle db. It connects just fine and everything works. I had inserted the following code to sense if the db connection has gone down and try to reconnect. It was working, so I thought and now works sporadically, mostly if I start adding random code. I think it is some sort of pointer problem. I would like some validation that I am using the code correctly while I go off and look for some hidden pointer issue. When the database is shutdown I get an indication that the connection was lost but this code does not always sense the lost connection. At times it will merrily go along and report that the connection is good. And never try to reconnect.

When Oracle is not running the SqlRecord request will print a message indicating no oracle connection and then the followup checks for connection indicate all is well Oracle is connected. If Oracle is not connected when I start the program then everything works fine. The lack of connection is sensed until there is one. Then if I disconnect the code behaves like it does not know it.

I'm using 3.3.4 on a Linux platform.

Thanks,
Rick



#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>

#include <qapplication.h>
#include "connectoracle.h"

#include <stdlib.h>

int main(int argc, char *argv[])
{
qWarning("Executing ConnectionTester");

QApplication a( argc, argv );

ConnectOracle nmb;

//a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );

return a.exec();

}


#include "connectoracle.h"
#include <qsqlrecord.h>

ConnectOracle::ConnectOracle()
{

int ProcessingFreq;

EstablishOracleConnection();


IdleTimer = new QTimer( this);

ProcessingFreq = 5000; //Default is 3 seconds

connect( IdleTimer, SIGNAL(timeout()), this, SLOT(IdleTimerTimeout()) );
IdleTimer->start( ProcessingFreq, TRUE );


qWarning("Timer initialization complete.");
}


ConnectOracle::~ConnectOracle()
{
}

void ConnectOracle::IdleTimerTimeout()
{


//Check if there are any messages on either the Notes or TCA table
if (DBReconnect())
{
;
}

IdleTimer->start( 5000, TRUE );
}

bool ConnectOracle::EstablishOracleConnection()
{
QString siteName;
QString userName;
QString passWord;
QString hostname;

siteName = getenv("OMS_DB");
userName = getenv("OMS_USER");
passWord = getenv("OMS_PWD");
hostname = getenv("OMS_HOST");

oracledb = QSqlDatabase::addDatabase( "QOCI8", "OMSDB" );
oracledb->setDatabaseName( siteName );
oracledb->setUserName( userName );
oracledb->setPassword( passWord );
oracledb->setHostName( hostname );

if ( ! oracledb->open() )
{
qWarning( "Failed to open database: " + oracledb->lastError().text() );
omsdb = QSqlDatabase::database("OMSDB" );
return FALSE;
}
else
{
qDebug("Oracle connection successful");

omsdb = QSqlDatabase::database("OMSDB" );

//setup queries
//PrepareSql();
}


return(true);
}

bool ConnectOracle::DBReconnect()
{
QString tabname;
tabname = "Notes_Messages";
QSqlRecord temp = omsdb->record( tabname );
//temp = omsdb->recordInfo ( "Notes_Messages" );
qWarning( "DBReconnect: " + omsdb->lastError().text() );
if (omsdb->isOpenError())
qWarning( "Database connection failed: " + omsdb->lastError().text() );

if ( !omsdb->isOpen() )
{

qDebug("Error: Database connection has been lost. Attempting reconnect.");

if (!omsdb->open())
{
qWarning( "Failed to open database: " + omsdb->lastError().text() );
return FALSE;
}
else
{
qWarning("Database reconnect successful.");
//PrepareSql();
}
}

return(TRUE);
}

#ifndef CONNECTORACLE_H
#define CONNECTORACLE_H

#include <qtimer.h>

#include <qsqldatabase.h>

/**
@author Rick Burge
*/
class ConnectOracle: public QObject
{
Q_OBJECT

public:
ConnectOracle();

~ConnectOracle();

private:
bool DBReconnect();
bool EstablishOracleConnection();

QTimer *IdleTimer;

QSqlDatabase *omsdb;
QSqlDatabase *oracledb;

private slots:
virtual void IdleTimerTimeout();
};

#endif