|
|
@ -148,33 +148,30 @@ namespace X1.Application.BackendServiceManager |
|
|
|
var repository = serviceProvider.GetRequiredService<IProtocolLogRepository>(); |
|
|
|
var unitOfWork = serviceProvider.GetRequiredService<IUnitOfWork>(); |
|
|
|
|
|
|
|
// 使用事务处理
|
|
|
|
using var transaction = await unitOfWork.BeginTransactionAsync(IsolationLevel.ReadCommitted, cancellationToken); |
|
|
|
try |
|
|
|
{ |
|
|
|
// 批量插入到数据库
|
|
|
|
await repository.AddRangeAsync(validLogs, cancellationToken); |
|
|
|
|
|
|
|
// 保存更改
|
|
|
|
await unitOfWork.SaveChangesAsync(cancellationToken); |
|
|
|
|
|
|
|
// 提交事务
|
|
|
|
await unitOfWork.CommitTransactionAsync(transaction, cancellationToken); |
|
|
|
|
|
|
|
_logger.LogDebug("协议日志批量插入数据库成功,数量:{Count}", validLogs.Count()); |
|
|
|
// 使用事务处理
|
|
|
|
await unitOfWork.ExecuteTransactionAsync(async () => |
|
|
|
{ |
|
|
|
// 批量插入到数据库
|
|
|
|
await repository.AddRangeAsync(validLogs, cancellationToken); |
|
|
|
|
|
|
|
// 保存更改
|
|
|
|
await unitOfWork.SaveChangesAsync(cancellationToken); |
|
|
|
|
|
|
|
_logger.LogDebug("协议日志批量插入数据库成功,数量:{Count}", validLogs.Count()); |
|
|
|
}, IsolationLevel.ReadCommitted, cancellationToken); |
|
|
|
} |
|
|
|
catch (OperationCanceledException) |
|
|
|
{ |
|
|
|
_logger.LogInformation("协议日志处理被取消"); |
|
|
|
await unitOfWork.RollbackTransactionAsync(cancellationToken); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "批量插入协议日志到数据库失败,数量:{Count}", validLogs.Count()); |
|
|
|
await unitOfWork.RollbackTransactionAsync(cancellationToken); |
|
|
|
|
|
|
|
// 如果批量插入失败,尝试逐个插入
|
|
|
|
await ProcessProtocolLogsIndividually(validLogs, cancellationToken); |
|
|
|
//await ProcessProtocolLogsIndividually(validLogs, cancellationToken);
|
|
|
|
} |
|
|
|
}, cancellationToken); |
|
|
|
|
|
|
@ -253,34 +250,34 @@ namespace X1.Application.BackendServiceManager |
|
|
|
} |
|
|
|
|
|
|
|
var batch = logs.Skip(i).Take(batchSize); |
|
|
|
using var transaction = await unitOfWork.BeginTransactionAsync(IsolationLevel.ReadCommitted, cancellationToken); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
foreach (var log in batch) |
|
|
|
await unitOfWork.ExecuteTransactionAsync(async () => |
|
|
|
{ |
|
|
|
try |
|
|
|
foreach (var log in batch) |
|
|
|
{ |
|
|
|
await repository.AddAsync(log, cancellationToken); |
|
|
|
successCount++; |
|
|
|
try |
|
|
|
{ |
|
|
|
await repository.AddAsync(log, cancellationToken); |
|
|
|
successCount++; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
errorCount++; |
|
|
|
_logger.LogError(ex, "插入单个协议日志失败,ID:{LogId},设备:{DeviceCode}", log.Id, log.DeviceCode); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
errorCount++; |
|
|
|
_logger.LogError(ex, "插入单个协议日志失败,ID:{LogId},设备:{DeviceCode}", log.Id, log.DeviceCode); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 保存当前批次的更改
|
|
|
|
await unitOfWork.SaveChangesAsync(cancellationToken); |
|
|
|
await unitOfWork.CommitTransactionAsync(transaction, cancellationToken); |
|
|
|
// 保存当前批次的更改
|
|
|
|
await unitOfWork.SaveChangesAsync(cancellationToken); |
|
|
|
}, IsolationLevel.ReadCommitted, cancellationToken); |
|
|
|
|
|
|
|
_logger.LogDebug("批次处理完成,成功:{SuccessCount},失败:{ErrorCount},批次大小:{BatchSize}", |
|
|
|
successCount, errorCount, batch.Count()); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
await unitOfWork.RollbackTransactionAsync(cancellationToken); |
|
|
|
_logger.LogError(ex, "批次处理失败,批次索引:{BatchIndex}", i / batchSize); |
|
|
|
errorCount += batch.Count(); |
|
|
|
} |
|
|
|