gpt4 book ai didi

postgresql - Flask-SQLAlchemy 中的自动提交

转载 作者:行者123 更新时间:2023-11-29 11:16:58 25 4
gpt4 key购买 nike

如何设置初始化我的 Flask 应用程序以将 Flask-SQLAlchemy 设置为自动提交模式,除非我显式 session.begin() 否则不使用事务?

session “begins a database transaction as soon as it starts communicating”。这是否会影响比 MySQL 更难的 Postgres?


通过 Instagram ,

autocommit mode; in this mode, Psycopg2 won’t issue BEGIN/COMMIT for any queries; instead, every query runs in its own single-statement transaction. This is particularly useful for read-only queries where transaction semantics aren’t needed. It’s as easy as doing:

connection.autocommit = True

This lowered chatter between our application servers and DBs significantly, and lowered system CPU as well on the database boxes

最佳答案

我想你可以在 Flask-SQLAlchemy 中设置 autocommit 这样做:

from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(session_options={'autocommit': True})

编辑:“flask.ext”。表单现在已删除(请参阅 here)

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(session_options={'autocommit': True})

关于postgresql - Flask-SQLAlchemy 中的自动提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25197871/

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