Skip to content

Commit

Permalink
Fix concurrent use of DbContext in Get Person API operation (#1673)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Nov 12, 2024
1 parent 878e814 commit d8a7ed5
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@ async Task<T> WithDataverseAdapterLock<T>(Func<Task<T>> action)
}
}

using var trsDbLock = new SemaphoreSlim(1, 1);

async Task<T> WithTrsDbLock<T>(Func<Task<T>> action)
{
await trsDbLock.WaitAsync();
try
{
return await action();
}
finally
{
trsDbLock.Release();
}
}

var contact = contactDetail.Contact;
var personId = contact.Id;

Expand Down Expand Up @@ -288,7 +303,7 @@ async Task<T> WithDataverseAdapterLock<T>(Func<Task<T>> action)
null;

var getMqsTask = command.Include.HasFlag(GetPersonCommandIncludes.MandatoryQualifications) ?
dbContext.MandatoryQualifications.Where(q => q.PersonId == personId).ToArrayAsync() :
WithTrsDbLock(() => dbContext.MandatoryQualifications.Where(q => q.PersonId == personId).ToArrayAsync()) :
null;

var getQualificationsTask = (command.Include & (GetPersonCommandIncludes.NpqQualifications | GetPersonCommandIncludes.HigherEducationQualifications)) != 0 ?
Expand Down Expand Up @@ -324,7 +339,7 @@ async Task<T> WithDataverseAdapterLock<T>(Func<Task<T>> action)
null;

var getAlertsTask = command.Include.HasFlag(GetPersonCommandIncludes.Sanctions) || command.Include.HasFlag(GetPersonCommandIncludes.Alerts) ?
dbContext.Alerts.Include(a => a.AlertType).ThenInclude(at => at.AlertCategory).Where(a => a.PersonId == contact.Id).ToArrayAsync() :
WithTrsDbLock(() => dbContext.Alerts.Include(a => a.AlertType).ThenInclude(at => at.AlertCategory).Where(a => a.PersonId == contact.Id).ToArrayAsync()) :
null;

IEnumerable<NameInfo>? previousNames = previousNameHelper.GetFullPreviousNames(contactDetail.PreviousNames, contactDetail.Contact)
Expand Down

0 comments on commit d8a7ed5

Please sign in to comment.