gpt4 book ai didi

python - 在 psycopg2 中,如何将 register_hstore 与连接池一起使用?

转载 作者:行者123 更新时间:2023-11-29 12:20:37 27 4
gpt4 key购买 nike

我在很多 python 项目中使用 psycopg2,并且经常使用 HSTORE 作为 postgres 数据类型。

通常我会按如下方式创建连接:

connection =psycopg2.connect(...) 
psycopg2.extras.register_hstore(connection)

然后允许 Python dicts 和 Postgres hstores 之间的无缝集成。

但是,如果我想使用连接池,显然我必须这样写:

pool = psycopg2.pool.SimpleConnectionPool(...)

然后使用它

connectionFromPool = pool.getconn()

但如果每次我请求它时都运行 register_hstore(connectionFromPool),那么该函数将在同一个连接上被调用多次。这会引起副作用吗?连接池的全部意义不就是避免不必要的调用吗?

有没有什么方法可以告诉 SimpleConnectionPool 在最初创建连接时使用 register_hstore

最佳答案

您可以使用psycopg2.extras.register_hstore(connection, global=True)

关于python - 在 psycopg2 中,如何将 register_hstore 与连接池一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26916030/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com