我遵循了 http://docs.pylonsproject.org/docs/pyramid/en/latest/tutorials/wiki/index.html 上的教程
我知道,当我添加或更改持久对象(在本例中为 Page 对象)时,更改将不会持久化,直到 transaction.commit()
被调用。为了取消更改,我可以调用 transaction.abort()
。
然而,在教程中,这些调用未显示在 View 可调用对象中。我假设有一些中间件会捕获异常并在发送 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 thetm.commit_veto
configuration setting was used, did the commit veto callback, called with the response generated by the application, return a result that evaluates toTrue
? if so,transaction.abort()
.If none of these checks calls
transaction.abort()
then the transaction is instead committed usingtransaction.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 的数字以启用它。
关于pyramid - pyramid什么时候提交zodb事务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29229348/