有没有C/C++库可用于postgres连接池?我看过pgpool,它更像是一个中间件。我正在寻找一个库,可以编码到我的应用程序中。
发布于 2011-06-28 18:19:58
没有一个很好的应用内连接池库。几乎整个社区都使用外部代理,特别是pgbouncer
,因为它具有额外的操作优势。同样,SOCI has a connection pool也是如此,但它的使用范围远不如pgbouncer
广泛。
发布于 2012-06-27 15:48:01
你看过libpqxx了吗?它本身并不是一个连接池,但是它提供了一个c++应用程序接口来从应用程序代码中抽象出连接处理。这使得应用程序可以很容易地构建和管理自己的连接池。
这真的很简单,这里有一个例子(使用boost for shared_ptr & pqxx)来演示一个池类,带有工厂方法。可以想象,runQuery方法将从指定的池中获取一个连接,并调用pqxx在底层连接上执行查询。然后,它可以将连接返回到池。
class DbPool {
public:
static db_handle_t create(const string &conn,
uint32_t max = DEFAULT_CON_MAX,
uint32_t min = DEFAULT_CON_MIN);
static pqxx::result runQuery(db_handle_t pool,
const string& query);
private:
DbPool(const string& con, uint32_t max_cons,
uint32_t min_cons);
static boost::ptr_vector<DbPool> pool_; // Keep a static vector of pools,
pqxx::connection *createCon();
void releaseCon(pqxx::connection *c);
uint32_t initializeCons();
pqxx::connection *getCon();
boost::ptr_list<pqxx::connection> m_freeCons;
}
https://stackoverflow.com/questions/6508037
复制