summaryrefslogtreecommitdiff
path: root/libpqpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-12-07 14:15:26 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2015-12-07 14:15:26 +0000
commitc0c79ad0667105e66666b5679a9264be58a92bb2 (patch)
tree100f783ce17497bf4bf5f7d066141f8359405bd7 /libpqpp
parentFix leak of error message in exception (diff)
downloadlibdbpp-postgresql-c0c79ad0667105e66666b5679a9264be58a92bb2.tar.bz2
libdbpp-postgresql-c0c79ad0667105e66666b5679a9264be58a92bb2.tar.xz
libdbpp-postgresql-c0c79ad0667105e66666b5679a9264be58a92bb2.zip
Improve connection failure detection
Diffstat (limited to 'libpqpp')
-rw-r--r--libpqpp/connection.cpp4
-rw-r--r--libpqpp/unittests/testpq.cpp38
2 files changed, 41 insertions, 1 deletions
diff --git a/libpqpp/connection.cpp b/libpqpp/connection.cpp
index 57531e3..de3f189 100644
--- a/libpqpp/connection.cpp
+++ b/libpqpp/connection.cpp
@@ -3,6 +3,7 @@
#include "selectcommand.h"
#include "modifycommand.h"
#include <unistd.h>
+#include <poll.h>
#include <boost/assert.hpp>
NAMEDFACTORY("postgresql", PQ::Connection, DB::ConnectionFactory);
@@ -108,7 +109,8 @@ PQ::Connection::bulkUpdateStyle() const
void
PQ::Connection::ping() const
{
- if (PQstatus(conn) != CONNECTION_OK) {
+ struct pollfd fd { PQsocket(conn), POLLRDHUP | POLLERR | POLLHUP | POLLNVAL, 0 };
+ if (PQstatus(conn) != CONNECTION_OK || poll(&fd, 1, 1)) {
if (inTx()) {
throw ConnectionError(conn);
}
diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp
index 002b18c..3f55c63 100644
--- a/libpqpp/unittests/testpq.cpp
+++ b/libpqpp/unittests/testpq.cpp
@@ -169,6 +169,44 @@ BOOST_AUTO_TEST_CASE( insertId )
delete ro;
}
+BOOST_AUTO_TEST_CASE( reconnect )
+{
+ auto ro = DB::MockDatabase::openConnectionTo("pqmock");
+ auto rok = DB::MockDatabase::openConnectionTo("pqmock");
+ auto pqconn = dynamic_cast<PQ::Connection *>(ro);
+ int pid1 = PQbackendPID(pqconn->conn);
+ BOOST_REQUIRE(pid1);
+ ro->ping();
+ auto kil = rok->newModifyCommand("SELECT pg_terminate_backend(?)");
+ kil->bindParamI(0, pid1);
+ kil->execute();
+ delete kil;
+ ro->ping();
+ int pid2 = PQbackendPID(pqconn->conn);
+ BOOST_REQUIRE(pid2);
+ BOOST_REQUIRE(pid1 != pid2);
+ delete ro;
+ delete rok;
+}
+
+BOOST_AUTO_TEST_CASE( reconnectInTx )
+{
+ auto ro = DB::MockDatabase::openConnectionTo("pqmock");
+ auto rok = DB::MockDatabase::openConnectionTo("pqmock");
+ auto pqconn = dynamic_cast<PQ::Connection *>(ro);
+ int pid1 = PQbackendPID(pqconn->conn);
+ BOOST_REQUIRE(pid1);
+ ro->ping();
+ ro->beginTx();
+ auto kil = rok->newModifyCommand("SELECT pg_terminate_backend(?)");
+ kil->bindParamI(0, pid1);
+ kil->execute();
+ delete kil;
+ BOOST_REQUIRE_THROW(ro->ping(), PQ::ConnectionError);
+ delete ro;
+ delete rok;
+}
+
BOOST_AUTO_TEST_SUITE_END();
BOOST_AUTO_TEST_CASE( connfail )