Skip to content

Commit 0764388

Browse files
committed
down ported double check pattern
1 parent adc3ff0 commit 0764388

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

CoreHelpers.WindowsAzure.Storage.Table/Extensions/TableClientExtensions.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,33 @@ public static async Task<Response<IReadOnlyList<Response>>> SubmitTransactionWit
4949
// check the exception
5050
if (allowAutoCreate && ex.ErrorCode.Equals("TableNotFound"))
5151
{
52-
// try to create the table
53-
await tc.CreateAsync();
52+
// This is a double check pattern to ensure that two independent processes
53+
// who are trying to create the table in parallel do not end up in an unhandled
54+
// situation.
55+
try
56+
{
57+
// try to create the table
58+
await tc.CreateAsync();
59+
}
60+
catch (TableTransactionFailedException doubleCheckEx)
61+
{
62+
// check if we have an errorCode if not the system throws the exception
63+
// to the caller
64+
if (String.IsNullOrEmpty(doubleCheckEx.ErrorCode))
65+
{
66+
ExceptionDispatchInfo.Capture(ex).Throw();
67+
return null;
68+
}
69+
70+
// Every error except the TableAlreadyExists is thrown to the caller but
71+
// in the case the system is trying to create the table in parallel we
72+
// ignore the error and execute the transaction!
73+
if (!doubleCheckEx.ErrorCode.Equals("TableAlreadyExists"))
74+
{
75+
ExceptionDispatchInfo.Capture(ex).Throw();
76+
return null;
77+
}
78+
}
5479

5580
// retry
5681
return await tc.SubmitTransactionAsync(transactionActions, cancellationToken);

0 commit comments

Comments
 (0)