Skip to content

Commit

Permalink
slightly better solution for recovering broken postgres connections
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Aug 3, 2011
1 parent 684398e commit b86243e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
32 changes: 22 additions & 10 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -439,14 +439,9 @@ bool QgsPostgresProvider::declareCursor(

if ( !connectionRO->openCursor( cursorName, query ) )
{
//try to re-etablish broken connection
::PQreset( connectionRO->pgConnection() );
if ( !connectionRO->openCursor( cursorName, query ) )
{
// reloading the fields might help next time around
rewind();
return false;
}
// reloading the fields might help next time around
rewind();
return false;
}
}
catch ( PGFieldNotFound )
Expand Down Expand Up @@ -3494,7 +3489,7 @@ bool QgsPostgresProvider::Conn::closeCursor( QString cursorName )
return true;
}

bool QgsPostgresProvider::Conn::PQexecNR( QString query )
bool QgsPostgresProvider::Conn::PQexecNR( QString query, bool retry )
{
Result res = ::PQexec( conn, query.toUtf8() );
if ( !res )
Expand Down Expand Up @@ -3524,7 +3519,24 @@ bool QgsPostgresProvider::Conn::PQexecNR( QString query )
openCursors = 0;
}

PQexecNR( "ROLLBACK" );
if ( PQstatus( conn ) == CONNECTION_OK )
{
PQexecNR( "ROLLBACK" );
}
else if ( retry )
{
QgsDebugMsg( "connection bad - resetting" );
::PQreset( conn );
if ( PQstatus( conn ) == CONNECTION_OK )
{
QgsDebugMsg( "reconnected - retrying" );
return PQexecNR( query, false );
}
}
else
{
QgsDebugMsg( "connection bad - giving up" );
}

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -609,7 +609,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
int pgVersion() { return postgresqlVersion; }

//! run a query and free result buffer
bool PQexecNR( QString query );
bool PQexecNR( QString query, bool retry = true );

//! cursor handling
bool openCursor( QString cursorName, QString declare );
Expand Down

0 comments on commit b86243e

Please sign in to comment.