GAE:如何回滚交易? [英] GAE: How to rollback a transaction?

查看:149
本文介绍了GAE:如何回滚交易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了GAE最佳做法的摘要: https://cloud. google.com/datastore/docs/best-practices

I just read this great summary of GAE best practices: https://cloud.google.com/datastore/docs/best-practices

其中之一是:

如果事务失败,请确保您尝试回滚事务. 回滚可最大程度地减少其他请求的重试延迟 在交易中争用相同资源.请注意 回滚本身可能会失败,因此回滚应尽力而为 只能尝试.

If a transaction fails, ensure you try to rollback the transaction. The rollback minimizes retry latency for a different request contending for the same resource(s) in a transaction. Note that a rollback itself may fail, so the rollback should be a best-effort attempt only.

我认为GAE会为您做交易回滚,但是上面的报价说您应该自己做.

I thought that transaction rollback was something that GAE did for you, but the above quote says that you should do it yourself.

此处的文档还说您应该回滚,但不说如何.

The documentation here also says you should do a rollback but does not say how.

那么,如何在GAE Python中回滚事务?

So, how do I rollback a transaction in GAE Python?

推荐答案

最佳做法文档是直接通过其API或客户端库使用Cloud Datastore.

The best practices document is for using the Cloud Datastore directly through its API or client libraries.

这仅在灵活的Appengine环境中是必需的.即使在这种情况下, Cloud Datastore客户端库仍提供了上下文管理器来自动处理回滚-此示例代码来自文档

This is only necessary in the flexible Appengine environment. Even in this case, the Cloud Datastore client library provides a context manager to automatically handle rollbacks - this example code is from the docs

def transfer_funds(client, from_key, to_key, amount):
    with client.transaction():
        from_account = client.get(from_key)
        to_account = client.get(to_key)

        from_account['balance'] -= amount
        to_account['balance'] += amount

        client.put_multi([from_account, to_account])

docs 状态:

默认情况下,如果交易块因错误退出而回退该交易

By default, the transaction is rolled back if the transaction block exits with an error

请注意,客户端库仍处于Beta版,因此该行为将来可能会更改.

在标准Appengine环境中,ndb库提供自动事务回滚:

In the standard Appengine environment, the ndb library provides automatic transaction rollback:

NDB客户端库可以在一个事务中对多个操作进行分组.除非事务中的每个操作都成功,否则事务无法成功;否则,事务将失败.如果任何操作失败,事务将自动回滚.

The NDB Client Libary can group multiple operations in a single transaction. The transaction cannot succeed unless every operation in the transaction succeeds; if any of the operations fail, the transaction is automatically rolled back.

这篇关于GAE:如何回滚交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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