Skip to content
Permalink
Browse files
slightly better solution for recovering broken postgres connections
  • Loading branch information
jef-n committed Aug 3, 2011
1 parent 684398e commit b86243ed9f6be9766284bc2e8ea82e7d4c0fbb5e
Showing with 23 additions and 11 deletions.
  1. +22 −10 src/providers/postgres/qgspostgresprovider.cpp
  2. +1 −1 src/providers/postgres/qgspostgresprovider.h
@@ -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 )
@@ -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 )
@@ -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;
}
@@ -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 );

0 comments on commit b86243e

Please sign in to comment.