postgis: allow for altering the default behavior of keeping open postgresql connections until the whole process ends - when 'persist_connection' = false new connections will be opened and closed for each datasource instance. (closes #434)

This commit is contained in:
Dane Springmeyer 2009-10-14 04:21:46 +00:00
parent eee88806b8
commit 60ca6a5677
4 changed files with 55 additions and 10 deletions

View file

@ -15,6 +15,9 @@ For a complete change history, see the SVN log.
Mapnik 0.6.2 Release
--------------------
- PostGIS: Add a 'persist_connection' option (default true), that when false will release
the idle psql connection after datasource goes out of scope (r1337) (#433,#434)
- Filters: Add support for '!=' as an alias to '<>' for not-equals filters (avoids &lt;&gt;) (r1326) (#427)
- Gdal Plugin: Add support for Gdal overviews, enabling fast loading of > 1GB rasters (r1321) (#54)

View file

@ -40,9 +40,11 @@ class Connection
private:
PGconn *conn_;
int cursorId;
bool closed_;
public:
Connection(std::string const& connection_str)
:cursorId(0)
:cursorId(0),
closed_(false)
{
conn_=PQconnectdb(connection_str.c_str());
if (PQstatus(conn_) != CONNECTION_OK)
@ -67,6 +69,7 @@ class Connection
PQclear(result);
return ok;
}
boost::shared_ptr<ResultSet> executeQuery(const std::string& sql,int type=0) const
{
PGresult *result=0;
@ -109,7 +112,14 @@ class Connection
void close()
{
PQfinish(conn_);
if (!closed_)
{
PQfinish(conn_);
#ifdef MAPNIK_DEBUG
std::clog << "PostGIS: datasource closed, also closing connection - " << conn_ << "\n";
#endif
closed_ = true;
}
}
std::string new_cursor_name()
@ -121,10 +131,14 @@ class Connection
~Connection()
{
PQfinish(conn_);
if (!closed_)
{
PQfinish(conn_);
#ifdef MAPNIK_DEBUG
std::clog << "close connection " << conn_ << "\n";
#endif
std::clog << "PostGIS: postgresql connection closed - " << conn_ << "\n";
#endif
closed_ = true;
}
}
};

View file

@ -76,11 +76,23 @@ postgis_datasource::postgis_datasource(parameters const& params)
params.get<std::string>("dbname"),
params.get<std::string>("user"),
params.get<std::string>("password")),
bbox_token_("!bbox!")
bbox_token_("!bbox!"),
persist_connection_(*params_.get<mapnik::boolean>("persist_connection",true))
{
if (table_.empty()) throw mapnik::datasource_exception("missing <table> parameter");
if (table_.empty()) throw mapnik::datasource_exception("PostGIS: missing <table> parameter");
#ifdef MAPNIK_DEBUG
if (persist_connection_)
{
clog << "PostGIS: persisting connection pool..." << endl;
}
else
{
clog << "PostGIS: not persisting connection..." << endl;
}
#endif
boost::optional<int> initial_size = params_.get<int>("inital_size",1);
boost::optional<int> max_size = params_.get<int>("max_size",10);
@ -257,7 +269,7 @@ postgis_datasource::postgis_datasource(parameters const& params)
case 25: // text
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::String));
break;
default: // shouldn't get here
default: // shouldn not get here
#ifdef MAPNIK_DEBUG
clog << "unknown type_oid="<<type_oid<<endl;
#endif
@ -490,4 +502,19 @@ Envelope<double> postgis_datasource::envelope() const
return extent_;
}
postgis_datasource::~postgis_datasource() {}
postgis_datasource::~postgis_datasource()
{
if (!persist_connection_)
{
ConnectionManager *mgr=ConnectionManager::instance();
shared_ptr<Pool<Connection,ConnectionCreator> > pool=mgr->getPool(creator_.id());
if (pool)
{
shared_ptr<Connection> conn = pool->borrowObject();
if (conn)
{
conn->close();
}
}
}
}

View file

@ -70,6 +70,7 @@ class postgis_datasource : public datasource
bool multiple_geometries_;
static const std::string name_;
const std::string bbox_token_;
bool persist_connection_;
public:
static std::string name();
int type() const;