如何在 Cloud Datastore 中使用事务
How to use transactions in Cloud Datastore
我想通过 Java 使用 Cloud Compute 中的 Datastore,我正在关注 Getting started with Google Cloud Datastore。
我的用例非常标准 - 读取一个实体(查找),修改它并保存新版本。我想在事务中执行此操作,这样如果有两个进程执行此操作,第二个进程将不会覆盖第一个进程所做的更改。
我成功地发起了一笔交易,并且成功了。但是我不知道如果交易失败会怎样:
- 如何识别交易失败?可能会抛出带有某些特定代码或名称的 DatastoreException?
- 我应该明确发出回滚吗?我可以假设如果交易失败,则不会写入任何内容吗?
- 我应该重试吗?
- 有这方面的文档吗?
How to identify a failed transaction? Probably a DatastoreException
with some specific code or name will be thrown?
您的代码应始终确保事务成功提交或回滚。这是一个例子:
// Begin the transaction.
BeginTransactionRequest begin = BeginTransactionRequest.newBuilder()
.build();
ByteString txn = datastore.beginTransaction(begin)
.getTransaction();
try {
// Zero or more transactional lookup()s or runQuerys().
// ...
// Followed by a commit().
CommitRequest commit = CommitRequest.newBuilder()
.setTransaction(txn)
.addMutation(...)
.build();
datastore.commit(commit);
} catch (Exception e) {
// If a transactional operation fails for any reason,
// attempt to roll back.
RollbackRequest rollback = RollbackRequest.newBuilder()
.setTransaction(txn);
.build();
try {
datastore.rollback(rollback);
} catch (DatastoreException de) {
// Rollback may fail due to a transient error or if
// the transaction was already committed.
}
// Propagate original exception.
throw e;
}
commit()
或 try
块内的另一个 lookup()
或 runQuery()
调用可能会引发异常。在每种情况下,清理事务都很重要。
Should I issue a rollback explicitly? Can I assume that if a
transaction fails, nothing from it will be written?
除非您确定 commit()
成功,否则您应该明确发出 rollback()
请求。但是,失败的 commit()
并不一定意味着没有写入数据。请参阅 this page.
上的注释
Should I retry?
您可以使用指数退避算法重试。但是,频繁的事务失败可能表明您尝试过于频繁地写入 entity group.
Is there any documentation on that?
https://cloud.google.com/datastore/docs/concepts/transactions
我想通过 Java 使用 Cloud Compute 中的 Datastore,我正在关注 Getting started with Google Cloud Datastore。
我的用例非常标准 - 读取一个实体(查找),修改它并保存新版本。我想在事务中执行此操作,这样如果有两个进程执行此操作,第二个进程将不会覆盖第一个进程所做的更改。
我成功地发起了一笔交易,并且成功了。但是我不知道如果交易失败会怎样:
- 如何识别交易失败?可能会抛出带有某些特定代码或名称的 DatastoreException?
- 我应该明确发出回滚吗?我可以假设如果交易失败,则不会写入任何内容吗?
- 我应该重试吗?
- 有这方面的文档吗?
How to identify a failed transaction? Probably a DatastoreException with some specific code or name will be thrown?
您的代码应始终确保事务成功提交或回滚。这是一个例子:
// Begin the transaction.
BeginTransactionRequest begin = BeginTransactionRequest.newBuilder()
.build();
ByteString txn = datastore.beginTransaction(begin)
.getTransaction();
try {
// Zero or more transactional lookup()s or runQuerys().
// ...
// Followed by a commit().
CommitRequest commit = CommitRequest.newBuilder()
.setTransaction(txn)
.addMutation(...)
.build();
datastore.commit(commit);
} catch (Exception e) {
// If a transactional operation fails for any reason,
// attempt to roll back.
RollbackRequest rollback = RollbackRequest.newBuilder()
.setTransaction(txn);
.build();
try {
datastore.rollback(rollback);
} catch (DatastoreException de) {
// Rollback may fail due to a transient error or if
// the transaction was already committed.
}
// Propagate original exception.
throw e;
}
commit()
或 try
块内的另一个 lookup()
或 runQuery()
调用可能会引发异常。在每种情况下,清理事务都很重要。
Should I issue a rollback explicitly? Can I assume that if a transaction fails, nothing from it will be written?
除非您确定 commit()
成功,否则您应该明确发出 rollback()
请求。但是,失败的 commit()
并不一定意味着没有写入数据。请参阅 this page.
Should I retry?
您可以使用指数退避算法重试。但是,频繁的事务失败可能表明您尝试过于频繁地写入 entity group.
Is there any documentation on that?
https://cloud.google.com/datastore/docs/concepts/transactions