GAE:如何回滚事务?
GAE: How to rollback a transaction?
我刚刚阅读了这篇关于 GAE 最佳实践的精彩总结: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为你做的,但上面的引述说你应该自己做。
文档 here 还说你应该回滚,但没有说明如何回滚。
那么,如何在 GAE 中回滚事务 Python?
最佳实践文档适用于直接通过其 API 或客户端库使用云数据存储。
这仅在灵活的 Appengine 环境中是必需的。即使在这种情况下,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
请注意,客户端库仍处于测试阶段,因此该行为将来可能会发生变化。
在标准Appengine环境下,ndb库提供automatic transaction rollback:
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 最佳实践的精彩总结: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为你做的,但上面的引述说你应该自己做。
文档 here 还说你应该回滚,但没有说明如何回滚。
那么,如何在 GAE 中回滚事务 Python?
最佳实践文档适用于直接通过其 API 或客户端库使用云数据存储。
这仅在灵活的 Appengine 环境中是必需的。即使在这种情况下,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
请注意,客户端库仍处于测试阶段,因此该行为将来可能会发生变化。
在标准Appengine环境下,ndb库提供automatic transaction rollback:
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.