pyramid什么时候提交zodb事务?

When does pyramid commit zodb transaction?

我遵循了 http://docs.pylonsproject.org/docs/pyramid/en/latest/tutorials/wiki/index.html

上的教程

我知道当我添加或更改持久对象(在本例中是 Page 对象)时,直到调用 transaction.commit() 时更改才会持久。为了取消更改,我可以调用 transaction.abort().

但是,在本教程中,这些调用未显示在视图可调用对象中。我假设有一些中间件会捕获异常并在发送 HTTP 响应之前调用 .abort() 或调用 .commit(),但我在代码或配置中的任何地方都没有看到任何提及文件。

你能给我指出正确的方向吗?我只需要知道幕后发生了什么,所以我知道是否需要自己添加一些东西

使用pyramid_tm package;它会安装一个管理事务的 Tween。

它只是为每个请求启动一个事务,如果请求成功则提交事务,否则中止。

来自文档:

At the beginning of a request a new transaction is started using the transaction.begin() function. Once the request has finished all of its works (ie views have finished running), a few checks are tested:

  • Did some a transaction.doom() cause the transaction to become “doomed”? if so, transaction.abort().
  • Did an exception occur in the underlying code? if so, transaction.abort() If the tm.commit_veto configuration setting was used, did the commit veto callback, called with the response generated by the application, return a result that evaluates to True? if so, transaction.abort().

If none of these checks calls transaction.abort() then the transaction is instead committed using transaction.commit().

如果出现可重试的异常(例如 ZODB 提交冲突),它还会重试 请求(从头重新启动它们):

When the transaction manager calls the downstream handler, if the handler raises a “retryable” exception, the transaction manager can be configured to attempt to call the downstream handler again with the same request, in effect “replaying” the request.

默认情况下禁用此行为;您可以将 tm.attempts 选项设置为大于 1 的数字以启用它。