Skip to content

Commit

Permalink
oracle provider: skip retrying unavailable connections for 30s
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Dec 5, 2016
1 parent 42b8ee9 commit b0126d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/providers/oracle/qgsoracleconn.cpp
Expand Up @@ -25,11 +25,11 @@

#include <QSettings>
#include <QSqlError>
#include <QDateTime>

QMap<QString, QgsOracleConn *> QgsOracleConn::sConnections;
int QgsOracleConn::snConnections = 0;
const int QgsOracleConn::sGeomTypeSelectLimit = 100;
QMap<QString, QDateTime> QgsOracleConn::sBrokenConnections;

QgsOracleConn *QgsOracleConn::connectDb( const QgsDataSourceURI& uri )
{
Expand Down Expand Up @@ -72,24 +72,44 @@ QgsOracleConn::QgsOracleConn( QgsDataSourceURI uri )
mDatabase.setUserName( uri.username() );
mDatabase.setPassword( uri.password() );

QgsDebugMsg( QString( "Connecting with options: " ) + options );
QString username = uri.username();
QString password = uri.password();

if ( !mDatabase.open() )
QString realm( database );
if ( !username.isEmpty() )
realm.prepend( username + "@" );

if ( sBrokenConnections.contains( realm ) )
{
QString username = uri.username();
QString password = uri.password();
QDateTime now( QDateTime::currentDateTime() );
QDateTime since( sBrokenConnections[ realm ] );
QgsDebugMsg( QString( "Broken since %1 [%2s ago]" ).arg( since.toString( Qt::ISODate ) ).arg( since.secsTo( now ) ) );

QString realm( database );
if ( !username.isEmpty() )
realm.prepend( username + "@" );
if ( since.secsTo( now ) < 30 )
{
QgsMessageLog::logMessage( tr( "Connection failed %1s ago - skipping retry" ).arg( since.secsTo( now ) ), tr( "Oracle" ) );
mRef = 0;
return;
}
}

QgsDebugMsg( QString( "Connecting with options: " ) + options );
if ( !mDatabase.open() )
{
QgsCredentials::instance()->lock();

while ( !mDatabase.open() )
{
bool ok = QgsCredentials::instance()->get( realm, username, password, mDatabase.lastError().text() );
if ( !ok )
{
QDateTime now( QDateTime::currentDateTime() );
QgsDebugMsg( QString( "get failed: %1 <= %2" ).arg( realm ).arg( now.toString( Qt::ISODate ) ) );
sBrokenConnections.insert( realm, now );
break;
}

sBrokenConnections.remove( realm );

if ( !username.isEmpty() )
{
Expand Down
2 changes: 2 additions & 0 deletions src/providers/oracle/qgsoracleconn.h
Expand Up @@ -24,6 +24,7 @@
#include <QMap>
#include <QSet>
#include <QThread>
#include <QDateTime>

#include "qgis.h"
#include "qgsdatasourceuri.h"
Expand Down Expand Up @@ -190,6 +191,7 @@ class QgsOracleConn : public QObject

static QMap<QString, QgsOracleConn *> sConnections;
static int snConnections;
static QMap<QString, QDateTime> sBrokenConnections;
};

#endif

0 comments on commit b0126d0

Please sign in to comment.