Skip to content

Commit 5b218dd

Browse files
committed
Added retries to LockDatabase.
1 parent a14e1df commit 5b218dd

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/Thinktecture.EntityFrameworkCore.SqlServer.Testing/EntityFrameworkCore/Testing/SqlServerTestDbContextProvider.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,19 +254,27 @@ protected virtual IDbContextTransaction BeginTransaction(T ctx)
254254

255255
CreateTestIsolationTable(ctx, lockTableName);
256256

257-
var tx = ctx.Database.BeginTransaction(IsolationLevel.Serializable);
258-
259-
try
260-
{
261-
LockDatabase(ctx, lockTableName);
262-
}
263-
catch (Exception)
257+
for (var i = 0;; i++)
264258
{
265-
tx.Dispose();
266-
throw;
267-
}
259+
var tx = ctx.Database.BeginTransaction(IsolationLevel.Serializable);
268260

269-
return tx;
261+
try
262+
{
263+
LockDatabase(ctx, lockTableName);
264+
return tx;
265+
}
266+
catch (Exception)
267+
{
268+
if (i > _maxNumberOfLockRetries)
269+
{
270+
tx.Dispose();
271+
throw;
272+
}
273+
274+
var delay = new TimeSpan(_random.NextInt64(_minRetryDelay.Ticks, _maxRetryDelay.Ticks));
275+
Task.Delay(delay).GetAwaiter().GetResult();
276+
}
277+
}
270278
}
271279

272280
private void CreateTestIsolationTable(T ctx, string lockTableName)

0 commit comments

Comments
 (0)