为什么 Sqlalchemy Session.close 不记录“回滚"? [英] Why do Sqlalchemy Session.close not log "rollback"?

查看:131
本文介绍了为什么 Sqlalchemy Session.close 不记录“回滚"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

# I've set echo=True when doing create_engine, so I can see all the sql stmt
# DBSession is ScopeSession(thread_local) and autocommit is False
session = DBSession() 
session.add(somemodel)
# 
try:
    session.flush()
    raise Exception()
    session.commit()
except SQLAlchemyError as e:
    session.rollback()
finally:
    session.close()

根据 SQLAlchemy 文档:

acording to the SQLAlchemy docs:

The close() method issues a expunge_all(), and releases any transactional/connection
resources. When connections are returned to the connection pool, transactional state is
rolled back as well.

我希望在执行session.close()"时看到日志回滚"

I expect to see the log "rollback" when executing "session.close()"

推荐答案

在池中发生的回滚(或如果已配置,提交)当前未参与引擎通常为提交/回滚事件执行的日志记录.

the rollback (or if configured, the commit) that occurs in the Pool is not currently participating in the logging that the Engine normally does for commit/rollback events.

票:http://www.sqlalchemy.org/trac/ticket/2752 已添加.

看看这个,我认为这个日志记录应该仍然是池记录器的一部分,而不是引擎的一部分,否则你会得到很多这样的:

Took a look at this, and I think this logging should still be part of the pool's logger, not the engine's, otherwise you get a lot of this:

sqlalchemy.Engine: COMMIT
sqlalchemy.Engine: ROLLBACK (via pool)

一个应用程序真的不应该太担心池的回滚,因为如果你使用的是 Session,你真的应该调用 commit()code> 或 rollback() 就在任何 close() 操作之前.

An application really shouldn't have to worry too much about the pool's rollback of things, because if you're using a Session, you really should be calling commit() or rollback() right before any close() operation.

通常通过 create_engine("url", echo_pool='debug') 或在 sqlalchemy.pool 命名空间上设置日志记录来打开池日志记录.

the pool logging is turned on normally by create_engine("url", echo_pool='debug'), or setting up logging on the sqlalchemy.pool namespace.

这篇关于为什么 Sqlalchemy Session.close 不记录“回滚"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆