6
votes

Suppose I have:

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);   
}

What I want is this: if insert fails, delete should be undone. Is this correct way of making transaction? Everything happens in same partition/table. Also what are other limitations of transactions, I've read somewhere that no more than 4 Mb could be stored within transaction, is this still correct?

1

1 Answers

12
votes

Assuming all the entities on which operations need to be performed have the same PartitionKey, you can make use of Entity Group Transaction functionality available in Windows Azure Table Storage. It does exactly that. If an operation on an entity in a transaction fails, the whole transaction is rolled back.

However it seems that you're deleting an entity and creating the same entity again. That scenario will not work in an entity batch transaction as an entity can appear only once in a transaction and only one operation can be performed on an entity. It looks like what you're interested in is replacing an entity. In that case, you can directly use InsertOrReplace() functionality.