How to locking database record for IsolationLevel RepeatableRead in ABP UnitOfWork?

Viewed 21

It's better if this issue is explained with an example. I have a database table VoucherNumber with an int column named [TotalNumberRecord]. It has only a record with the initial value of TotalNumberRecord == 0.

In my VoucherNumberAppService.cs, there are the following 2 methods

        public async void TestIncrementA(string id)
        {
            using var unitOfWork = _unitOfWorkManager.Begin(isolationLevel: System.Data.IsolationLevel.RepeatableRead);
            var query = await _voucherNumberService.GetQueryableAsync();
            query = query.Where(p => p.Id == id);
            var sections = await AsyncExecuter.FirstOrDefaultAsync(query);
            sections.TotalNumberRecord++;
            await Task.Delay(5000);
            await _voucherNumberService.UpdateAsync(sections);
            await unitOfWork.CompleteAsync();
        }
        public async void TestIncrementB(string id)
        {
            using var unitOfWork = _unitOfWorkManager.Begin(isolationLevel: System.Data.IsolationLevel.RepeatableRead);
            var query = await _voucherNumberService.GetQueryableAsync();
            query = query.Where(p => p.Id == id);
            var sections = await AsyncExecuter.FirstOrDefaultAsync(query);
            sections.TotalNumberRecord++;
            await _voucherNumberService.UpdateAsync(sections);
            await unitOfWork.CompleteAsync();
        }

The 2 methods are essentially the same which increment the value of the column TotalNumberRecord by one except that the first method delays the thread.

Now I run 2 methods

TestIncrementA(id);
TestIncrementB(id);

I would expect the value of TotalNumberRecord in my database to be 2 now since it's been incremented twice. However it's only 1. Is there something that I'm overlooking?

0 Answers
Related