Tal*_*mad 3 python session sqlalchemy
# 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()
Run Code Online (Sandbox Code Playgroud)
根据 SQLAlchemy 文档:
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.
Run Code Online (Sandbox Code Playgroud)
我希望在执行“session.close()”时看到日志“rollback”
池中发生的回滚(或提交)当前不参与引擎通常为提交/回滚事件执行的日志记录。
票证: http: //www.sqlalchemy.org/trac/ticket/2752已添加。
编辑:看了一下这个,我认为这个日志记录应该仍然是池记录器的一部分,而不是引擎的,否则你会得到很多这样的信息:
sqlalchemy.Engine: COMMIT
sqlalchemy.Engine: ROLLBACK (via pool)
Run Code Online (Sandbox Code Playgroud)
应用程序实际上不必过多担心池的回滚,因为如果您使用Session,您确实应该在任何操作之前调用commit()or 。rollback()close()
池日志记录通常通过create_engine("url", echo_pool='debug')或在sqlalchemy.pool命名空间上设置日志记录来打开。
| 归档时间: |
|
| 查看次数: |
3110 次 |
| 最近记录: |