假设我有:
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,这仍然正确吗?
Answer recommended by
Microsoft Azure /collectives/azure
Collective
假设所有需要执行操作的实体都有相同的PartitionKey,您可以使用Entity Group Transaction http://msdn.microsoft.com/en-us/library/windowsazure/dd894038.aspxWindows Azure 表存储中提供的功能。它正是这样做的。如果事务中对实体的操作失败,则整个事务将回滚。
然而,您似乎正在删除一个实体并再次创建相同的实体。该场景在实体批量事务中不起作用,因为实体在事务中只能出现一次,并且只能对实体执行一个操作。看来您感兴趣的是替换实体。在这种情况下,您可以直接使用InsertOrReplace() http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.tableoperation.insertorreplace.aspx功能。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)