netcorepal / netcorepal-cloud-framework

基于 ASP.NET Core 的领域驱动设计微服务架构实现方案
https://netcorepal.github.io/netcorepal-cloud-framework/
MIT License
124 stars 36 forks source link

CommandUnitOfWorkBehavior 中 CommitAsync 为何要执行两遍? #18

Closed yc-2503 closed 7 months ago

yc-2503 commented 8 months ago

class CommandUnitOfWorkBehavior 中 第 54 行

            await using var transaction = _unitOfWork.BeginTransaction();
            {
                try
                {
                    WriteCommandBegin(new CommandBegin(id, commandName, request));
                    var response = await next();
                    WriteCommandEnd(new CommandEnd(id, commandName, request));
                    await _unitOfWork.SaveEntitiesAsync(cancellationToken);
                    await _unitOfWork.CommitAsync(cancellationToken); 
                    return response;
                }
                catch (Exception e)
                {
                    WriteCommandError(new CommandError(id, commandName, request, e));
                    await _unitOfWork.RollbackAsync(cancellationToken);
                    throw;
                }
            }

_unitOfWork.SaveEntitiesAsync 中已经执行了CommitAsync ,为何这里还要再执行一遍

witskeeper commented 8 months ago

_unitOfWork.SaveEntitiesAsync仅当未开启事物时,才会内部开启事物并在执行结束后提交事物。 在外部开启的事物,它是不会提交事物的,因此需要在外部提交事物。