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


Qt Code:
  1. #ifdef HAVE_CONFIG_H
  2. #include <config.h>
  3. #endif
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #include <qapplication.h>
  9. #include "connectoracle.h"
  10.  
  11. #include <stdlib.h>
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. qWarning("Executing ConnectionTester");
  16.  
  17. QApplication a( argc, argv );
  18.  
  19. ConnectOracle nmb;
  20.  
  21. //a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
  22.  
  23. return a.exec();
  24.  
  25. }
  26.  
  27.  
  28. #include "connectoracle.h"
  29. #include <qsqlrecord.h>
  30.  
  31. ConnectOracle::ConnectOracle()
  32. {
  33.  
  34. int ProcessingFreq;
  35.  
  36. EstablishOracleConnection();
  37.  
  38.  
  39. IdleTimer = new QTimer( this);
  40.  
  41. ProcessingFreq = 5000; //Default is 3 seconds
  42.  
  43. connect( IdleTimer, SIGNAL(timeout()), this, SLOT(IdleTimerTimeout()) );
  44. IdleTimer->start( ProcessingFreq, TRUE );
  45.  
  46.  
  47. qWarning("Timer initialization complete.");
  48. }
  49.  
  50.  
  51. ConnectOracle::~ConnectOracle()
  52. {
  53. }
  54.  
  55. void ConnectOracle::IdleTimerTimeout()
  56. {
  57.  
  58.  
  59. //Check if there are any messages on either the Notes or TCA table
  60. if (DBReconnect())
  61. {
  62. ;
  63. }
  64.  
  65. IdleTimer->start( 5000, TRUE );
  66. }
  67.  
  68. bool ConnectOracle::EstablishOracleConnection()
  69. {
  70. QString siteName;
  71. QString userName;
  72. QString passWord;
  73. QString hostname;
  74.  
  75. siteName = getenv("OMS_DB");
  76. userName = getenv("OMS_USER");
  77. passWord = getenv("OMS_PWD");
  78. hostname = getenv("OMS_HOST");
  79.  
  80. oracledb = QSqlDatabase::addDatabase( "QOCI8", "OMSDB" );
  81. oracledb->setDatabaseName( siteName );
  82. oracledb->setUserName( userName );
  83. oracledb->setPassword( passWord );
  84. oracledb->setHostName( hostname );
  85.  
  86. if ( ! oracledb->open() )
  87. {
  88. qWarning( "Failed to open database: " + oracledb->lastError().text() );
  89. omsdb = QSqlDatabase::database("OMSDB" );
  90. return FALSE;
  91. }
  92. else
  93. {
  94. qDebug("Oracle connection successful");
  95.  
  96. omsdb = QSqlDatabase::database("OMSDB" );
  97.  
  98. //setup queries
  99. //PrepareSql();
  100. }
  101.  
  102.  
  103. return(true);
  104. }
  105.  
  106. bool ConnectOracle::DBReconnect()
  107. {
  108. QString tabname;
  109. tabname = "Notes_Messages";
  110. QSqlRecord temp = omsdb->record( tabname );
  111. //temp = omsdb->recordInfo ( "Notes_Messages" );
  112. qWarning( "DBReconnect: " + omsdb->lastError().text() );
  113. if (omsdb->isOpenError())
  114. qWarning( "Database connection failed: " + omsdb->lastError().text() );
  115.  
  116. if ( !omsdb->isOpen() )
  117. {
  118.  
  119. qDebug("Error: Database connection has been lost. Attempting reconnect.");
  120.  
  121. if (!omsdb->open())
  122. {
  123. qWarning( "Failed to open database: " + omsdb->lastError().text() );
  124. return FALSE;
  125. }
  126. else
  127. {
  128. qWarning("Database reconnect successful.");
  129. //PrepareSql();
  130. }
  131. }
  132.  
  133. return(TRUE);
  134. }
  135.  
  136. #ifndef CONNECTORACLE_H
  137. #define CONNECTORACLE_H
  138.  
  139. #include <qtimer.h>
  140.  
  141. #include <qsqldatabase.h>
  142.  
  143. /**
  144. @author Rick Burge
  145. */
  146. class ConnectOracle: public QObject
  147. {
  148. Q_OBJECT
  149.  
  150. public:
  151. ConnectOracle();
  152.  
  153. ~ConnectOracle();
  154.  
  155. private:
  156. bool DBReconnect();
  157. bool EstablishOracleConnection();
  158.  
  159. QTimer *IdleTimer;
  160.  
  161. QSqlDatabase *omsdb;
  162. QSqlDatabase *oracledb;
  163.  
  164. private slots:
  165. virtual void IdleTimerTimeout();
  166. };
  167.  
  168. #endif
To copy to clipboard, switch view to plain text mode