diff --git a/plugins/input/postgis/connection.hpp b/plugins/input/postgis/connection.hpp index 5f8bf34dd..94086fb72 100644 --- a/plugins/input/postgis/connection.hpp +++ b/plugins/input/postgis/connection.hpp @@ -71,6 +71,7 @@ public: } s << "\n" << connection_str; + PQfinish(conn_); throw mapnik::datasource_exception(s.str()); } } @@ -152,7 +153,7 @@ public: bool isOK() const { - return (PQstatus(conn_) != CONNECTION_BAD); + return (!closed_) && (PQstatus(conn_) != CONNECTION_BAD); } void close() diff --git a/plugins/input/postgis/postgis_datasource.cpp b/plugins/input/postgis/postgis_datasource.cpp index 33df22924..307bdb5d8 100644 --- a/plugins/input/postgis/postgis_datasource.cpp +++ b/plugins/input/postgis/postgis_datasource.cpp @@ -127,11 +127,12 @@ void postgis_datasource::bind() const if (pool) { shared_ptr conn = pool->borrowObject(); - if (conn && conn->isOK()) - { - PoolGuard, - shared_ptr< Pool > > guard(conn, pool); + if (!conn) return; + PoolGuard, + shared_ptr< Pool > > guard(conn, pool); + if (conn->isOK()) + { desc_.set_encoding(conn->client_encoding()); if (geometry_table_.empty()) @@ -435,6 +436,7 @@ postgis_datasource::~postgis_datasource() if (conn) { conn->close(); + pool->returnObject(conn); } } } @@ -706,7 +708,17 @@ featureset_ptr postgis_datasource::features(const query& q) const } else { - throw mapnik::datasource_exception("Postgis Plugin: bad connection"); + std::string err_msg = "Postgis Plugin:"; + if (conn) + { + err_msg += " Bad connection"; + pool->returnObject(conn); + } + else + { + err_msg += " Null connection"; + } + throw mapnik::datasource_exception(err_msg); } } @@ -729,10 +741,11 @@ featureset_ptr postgis_datasource::features_at_point(coord2d const& pt) const if (pool) { shared_ptr conn = pool->borrowObject(); - if (conn && conn->isOK()) - { - PoolGuard, shared_ptr< Pool > > guard(conn, pool); + if (!conn) return featureset_ptr(); + PoolGuard, shared_ptr< Pool > > guard(conn, pool); + if (conn->isOK()) + { if (geometryColumn_.empty()) { std::ostringstream s_error; @@ -819,10 +832,10 @@ box2d postgis_datasource::envelope() const if (pool) { shared_ptr conn = pool->borrowObject(); - if (conn && conn->isOK()) + if (!conn) return extent_; + PoolGuard, shared_ptr< Pool > > guard(conn, pool); + if (conn->isOK()) { - PoolGuard, shared_ptr< Pool > > guard(conn, pool); - std::ostringstream s; boost::optional estimate_extent = @@ -920,10 +933,10 @@ boost::optional postgis_datasource::get_geometry if (pool) { shared_ptr conn = pool->borrowObject(); - if (conn && conn->isOK()) + if (!conn) return result; + PoolGuard, shared_ptr< Pool > > guard(conn, pool); + if (conn->isOK()) { - PoolGuard, shared_ptr< Pool > > guard(conn, pool); - std::ostringstream s; std::string g_type;