Skip to content

Commit b86243e

Browse files
committedAug 3, 2011
slightly better solution for recovering broken postgres connections
1 parent 684398e commit b86243e

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed
 

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,9 @@ bool QgsPostgresProvider::declareCursor(
439439

440440
if ( !connectionRO->openCursor( cursorName, query ) )
441441
{
442-
//try to re-etablish broken connection
443-
::PQreset( connectionRO->pgConnection() );
444-
if ( !connectionRO->openCursor( cursorName, query ) )
445-
{
446-
// reloading the fields might help next time around
447-
rewind();
448-
return false;
449-
}
442+
// reloading the fields might help next time around
443+
rewind();
444+
return false;
450445
}
451446
}
452447
catch ( PGFieldNotFound )
@@ -3494,7 +3489,7 @@ bool QgsPostgresProvider::Conn::closeCursor( QString cursorName )
34943489
return true;
34953490
}
34963491

3497-
bool QgsPostgresProvider::Conn::PQexecNR( QString query )
3492+
bool QgsPostgresProvider::Conn::PQexecNR( QString query, bool retry )
34983493
{
34993494
Result res = ::PQexec( conn, query.toUtf8() );
35003495
if ( !res )
@@ -3524,7 +3519,24 @@ bool QgsPostgresProvider::Conn::PQexecNR( QString query )
35243519
openCursors = 0;
35253520
}
35263521

3527-
PQexecNR( "ROLLBACK" );
3522+
if ( PQstatus( conn ) == CONNECTION_OK )
3523+
{
3524+
PQexecNR( "ROLLBACK" );
3525+
}
3526+
else if ( retry )
3527+
{
3528+
QgsDebugMsg( "connection bad - resetting" );
3529+
::PQreset( conn );
3530+
if ( PQstatus( conn ) == CONNECTION_OK )
3531+
{
3532+
QgsDebugMsg( "reconnected - retrying" );
3533+
return PQexecNR( query, false );
3534+
}
3535+
}
3536+
else
3537+
{
3538+
QgsDebugMsg( "connection bad - giving up" );
3539+
}
35283540

35293541
return false;
35303542
}

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
609609
int pgVersion() { return postgresqlVersion; }
610610

611611
//! run a query and free result buffer
612-
bool PQexecNR( QString query );
612+
bool PQexecNR( QString query, bool retry = true );
613613

614614
//! cursor handling
615615
bool openCursor( QString cursorName, QString declare );

0 commit comments

Comments
 (0)
Please sign in to comment.