Skip to content

Commit

Permalink
oracle provider: skip retrying unavailable connections for 30s
Browse files Browse the repository at this point in the history
(cherry picked from commit b0126d0)
  • Loading branch information
jef-n committed May 18, 2017
1 parent 38ed0e5 commit 4e47ce0
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
#include "qgssettings.h"

#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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QSet>
#include <QThread>
#include <QVariant>
#include <QDateTime>

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

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

#endif

0 comments on commit 4e47ce0

Please sign in to comment.