postgres 连接池库

发布于 2024-11-17 16:13:21 字数 80 浏览 1 评论 0原文

是否有可用于 postgres 连接池的 C/C++ 库?我研究过 pgpool,它更像是一个中间件。我正在寻找一个可以编码到我的应用程序中的库。

Is any C/C++ library available for postgres connection pooling? I have looked at pgpool which is more like a middleware. I am looking for a library which can be coded into my application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戒ㄋ 2024-11-24 16:13:21

您看过 libpqxx 了吗?它本身不是一个连接池,但它提供了一个 C++ API 来从应用程序代码中抽象出连接处理。这允许应用程序非常轻松地构建和管理自己的连接池。

这真的非常简单,这里有一个示例(使用 boost 来实现shared_ptr 和 pqxx)来说明带有工厂方法的池类。您可以想象 runQuery 方法将从指定的池中获取连接,并调用 pqxx API 在底层连接上执行查询。然后它可以将连接返回到池中。

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;

}

Have you looked at libpqxx yet? It's not a connection pooler per se, however it provides a c++ API to abstract the connection handling from the app code. This allows an app to build and manage its own connection pool quite easily.

It's really pretty simple, here's an example (using boost for shared_ptr & pqxx) to illustrate a pool class, with factory method. You can imagine that the runQuery method would get a connection from the pool specified, and call the pqxx APIs to execute the query on the underlying connection. It can then return the connection to the pool.

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;

}
沫雨熙 2024-11-24 16:13:21

没有一个好的应用内连接池库。几乎整个社区都使用外部代理,特别是 pgbouncer 由于其额外的运营效益。与此同时,SOCI 有一个连接池,但它几乎不被用作广泛称为 pgbouncer。

There isn't a good in-app connection pooling library. Nearly the entire community uses external proxies, notably pgbouncer due to its extra operational benefits. In the same breath, SOCI has a connection pool, but it isn't used nearly as widely as pgbouncer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文