决因为数据库连接问题导致的插入数据库异常

This commit is contained in:
2026-05-07 09:51:29 +08:00
parent 413f93c473
commit 4fce0df8ed
4 changed files with 36 additions and 16 deletions

View File

@@ -51,6 +51,12 @@ import java.util.regex.Pattern;
public class LbDailyUserTradeServiceImpl extends ServiceImpl<LbDailyUserTradeMapper, LbDailyUserTrade> implements ILbDailyUserTradeService {
private static final Logger log = LoggerFactory.getLogger(LbDailyUserTradeServiceImpl.class);
private static final BigDecimal SERVICE_RATE = new BigDecimal("0.01");
/**
* MyBatis-Plus {@code saveBatch(list)} uses one SqlSession for the whole list and only closes after commit.
* Large imports can exceed Hikari {@code leakDetectionThreshold} (borrow time), producing false leak warnings.
* Chunking yields shorter per-connection holds and matches pool-friendly batch sizing.
*/
private static final int EXCEL_IMPORT_SAVE_CHUNK_SIZE = 1000;
@Autowired
private ILbDailyUserTradeReportService lbDailyUserTradeReportService;
@@ -263,12 +269,20 @@ public class LbDailyUserTradeServiceImpl extends ServiceImpl<LbDailyUserTradeMap
return result;
}
boolean saved = this.saveBatch(importList);
if (!saved) {
log.error("导入数据保存失败fileName={}, importCount={}", originalFilename, importList.size());
result.put("success", false);
result.put("message", "导入失败,数据库保存异常");
return result;
int total = importList.size();
for (int from = 0; from < total; from += EXCEL_IMPORT_SAVE_CHUNK_SIZE) {
int to = Math.min(from + EXCEL_IMPORT_SAVE_CHUNK_SIZE, total);
List<LbDailyUserTrade> chunk = importList.subList(from, to);
if (!this.saveBatch(chunk)) {
log.error("导入数据保存失败fileName={}, importCount={}, failedChunk=[{}, {})",
originalFilename, total, from, to);
result.put("success", false);
result.put("message", "导入失败,数据库保存异常(批次失败,可能已写入部分数据)");
result.put("importCount", from);
result.put("skipCount", skippedRows.size());
result.put("skippedRows", skippedRows);
return result;
}
}
log.info("导入成功fileName={}, importCount={}, skipCount={}",