Discussion:
[soci-users] Create soci::session while reusing existing connection (PGconn)
Patrick Rotsaert
2013-04-23 05:05:24 UTC
Permalink
Hi,

I'd like to use soci in an application plugin that receives a PGconn (postgresql libpq) connection as a parameter. Ideally, this connection would be reused, so that no extra connection needs to be made.

There does not appear to be a way to instantiate a soci::session from a PGconn*. I tried to subclass soci::postgresql_session_backend and soci::postgresql_backend_factory, but the soci::postgresql_session_backend ctor will always setup a new connection.
The quickest fix would be to add a protected ctor:

struct postgresql_session_backend : details::session_backend
{
...
protected:
postgresql_session_backend(PGconn * conn) : statementCount_(0), conn_(conn) {}
};

This way, I could subclass the session and factory backends. The session destructor would set conn_ = 0, so that postgresql_session_backend::clean_up would not close the connection.

Any other ideas perhaps?

Thx, Pat
Mateusz Loskot
2013-04-23 10:15:35 UTC
Permalink
Post by Patrick Rotsaert
I'd like to use soci in an application plugin that receives a PGconn (postgresql libpq) connection as a parameter. Ideally, this connection would be reused, so that no extra connection needs to be made.
There does not appear to be a way to instantiate a soci::session from a PGconn*. I tried to subclass soci::postgresql_session_backend and soci::postgresql_backend_factory, but the soci::postgresql_session_backend ctor will always setup a new connection.
struct postgresql_session_backend : details::session_backend
{
...
postgresql_session_backend(PGconn * conn) : statementCount_(0), conn_(conn) {}
};
This way, I could subclass the session and factory backends. The session destructor would set conn_ = 0, so that postgresql_session_backend::clean_up would not close the connection.
Any other ideas perhaps?
There is no support for plugging in session/connection obtained
directly from low-level client API.

You need to patch SOCI to suit your needs.

Best regards,
--
Mateusz Loskot, http://mateusz.loskot.net
Loading...