Skip to main content
 首页 » 编程设计

python之扭曲的 adbapi 关闭自动提交(psycopg2)

2025年05月04日10zfyouxi

我插入了很多行,但似乎 postgress 跟不上。我用谷歌搜索了一下,建议您可以关闭自动提交。我不需要立即提交这些值。如果出现问题,我可以再次获取这些数据。

现在,当我搜索关闭自动提交时,我找不到我要找的东西。我尝试在 dbpool 构造函数中提供 autocommit=False:

dbpool = adbapi.ConnectionPool('psycopg2', user="xxx", password="xxx", database="postgres", host="localhost", autocommit=False) 

2013-01-27 18:24:42,254 - collector.EventLogItemController - 警告 - [失败实例:回溯::无效的连接选项“自动提交”

请您参考如下方法:

psycopg2 不声称支持 connect 的 autocommit 关键字参数:

connect(dsn=None, database=None, user=None, password=None, host=None, port=None, connection_factory=None, async=False, **kwargs) 
    Create a new database connection. 
 
    The connection parameters can be specified either as a string: 
 
        conn = psycopg2.connect("dbname=test user=postgres password=secret") 
 
    or using a set of keyword arguments: 
 
        conn = psycopg2.connect(database="test", user="postgres", password="secret") 
 
    The basic connection parameters are: 
 
    - *dbname*: the database name (only in dsn string) 
    - *database*: the database name (only as keyword argument) 
    - *user*: user name used to authenticate 
    - *password*: password used to authenticate 
    - *host*: database host address (defaults to UNIX socket if not provided) 
    - *port*: connection port number (defaults to 5432 if not provided) 
 
    Using the *connection_factory* parameter a different class or connections 
    factory can be specified. It should be a callable object taking a dsn 
    argument. 
 
    Using *async*=True an asynchronous connection will be created. 
 
    Any other keyword parameter will be passed to the underlying client 
    library: the list of supported parameter depends on the library version. 

当前的 postgresql 文档也没有讨论任何“自动提交”参数:

http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING 

所以问题可能在于这不是为 psycopg2 连接禁用自动提交的正确方法。除此之外,您不会发现关闭自动提交实际上对您有任何帮助。 adbapi.ConnectionPool 将开始并为您提交显式事务,回避 autocommit 模式可能给您带来的任何行为。