From 8e12a8ef84810c99f1a13b198a30f231a4e1f9d3 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 3 Jul 2013 07:36:15 -0700 Subject: [PATCH] when we get a fatal error from postgres server, try killing then recreating the connection. This is untested, as fatal errors are rare. --- xwords4/relay/dbmgr.cpp | 13 +++++++++++++ xwords4/relay/dbmgr.h | 1 + 2 files changed, 14 insertions(+) diff --git a/xwords4/relay/dbmgr.cpp b/xwords4/relay/dbmgr.cpp index f1b173404..ae080075d 100644 --- a/xwords4/relay/dbmgr.cpp +++ b/xwords4/relay/dbmgr.cpp @@ -762,6 +762,7 @@ DBMgr::execSql( const char* const query ) logf( XW_LOGERROR, "%s: PQexec=>%s;%s", __func__, PQresStatus(PQresultStatus(result)), PQresultErrorMessage(result) ); + clearThreadConn(); usleep( 20000 ); } PQclear( result ); @@ -1143,6 +1144,18 @@ destr_function( void* conn ) PQfinish( pgconn ); } +void +DBMgr::clearThreadConn() +{ + logf( XW_LOGERROR, "%s called()", __func__ ); + PGconn* conn = (PGconn*)pthread_getspecific( m_conn_key ); + if ( NULL != conn ) { + PQfinish( conn ); + int result = pthread_setspecific( m_conn_key, NULL ); + assert( 0 == result ); + } +} + PGconn* DBMgr::getThreadConn( void ) { diff --git a/xwords4/relay/dbmgr.h b/xwords4/relay/dbmgr.h index 70cf50ff5..bed8a96c6 100644 --- a/xwords4/relay/dbmgr.h +++ b/xwords4/relay/dbmgr.h @@ -135,6 +135,7 @@ class DBMgr { int byteaIndex, unsigned char* buf, size_t* buflen ); PGconn* getThreadConn( void ); + void clearThreadConn(); void conn_key_alloc(); pthread_key_t m_conn_key;