假设我有:
using (TransactionScope scope = new TransactionScope())
{
if (IndexExists(index.RowKey))
DeleteIndex(index.RowKey); //deletes using TableOperation.Delete
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference(Const.IndexTable);
TableOperation insertOperation = TableOperation.Insert(index);
table.Execute(insertOperation);
}
我想要的是:如果插入失败,删除应该被撤消。这是正确的交易方式吗?一切都发生在同一个分区/表中。另外,事务的其他限制是什么,我在某处读到事务中不能存储超过 4 Mb,这仍然正确吗?
最佳答案
假设所有需要操作的实体都有相同的PartitionKey,则可以使用 Entity Group Transaction
Windows Azure 表存储中提供的功能。它正是这样做的。如果事务中对实体的操作失败,则整个事务将回滚。
但是,您似乎正在删除一个实体并再次创建相同的实体。该场景在实体批量事务中不起作用,因为实体在事务中只能出现一次,并且只能对实体执行一个操作。看来您感兴趣的是替换实体。这种情况下,您可以直接使用 InsertOrReplace()
功能。
关于azure - Azure 表存储中的事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15309294/