事务回滚判断优化
This commit is contained in:
@@ -163,12 +163,14 @@ class CheckpointService(BaseService[Checkpoint]):
|
|||||||
logger.error(f"Failed to insert batch: {str(e)}")
|
logger.error(f"Failed to insert batch: {str(e)}")
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
# 如果有失败记录,不提交事务
|
# 如果有插入失败记录(不是跳过记录),不提交事务
|
||||||
if failed_items:
|
# 跳过记录不应该影响事务,只插入失败的记录才需要回滚
|
||||||
|
insert_failed_items = [item for item in failed_items if '插入失败' in item.get('error', '')]
|
||||||
|
if insert_failed_items:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
return {
|
return {
|
||||||
'success': False,
|
'success': False,
|
||||||
'message': f'批量导入失败: {len(failed_items)}条记录处理失败',
|
'message': f'批量导入失败: {len(insert_failed_items)}条记录插入失败',
|
||||||
'total_count': total_count,
|
'total_count': total_count,
|
||||||
'success_count': success_count,
|
'success_count': success_count,
|
||||||
'failed_count': failed_count,
|
'failed_count': failed_count,
|
||||||
|
|||||||
@@ -191,12 +191,14 @@ class LevelDataService(BaseService[LevelData]):
|
|||||||
logger.error(f"Failed to insert batch: {str(e)}")
|
logger.error(f"Failed to insert batch: {str(e)}")
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
# 如果有失败记录,不提交事务
|
# 如果有插入失败记录(不是跳过记录),不提交事务
|
||||||
if failed_items:
|
# 跳过记录不应该影响事务,只插入失败的记录才需要回滚
|
||||||
|
insert_failed_items = [item for item in failed_items if '插入失败' in item.get('error', '')]
|
||||||
|
if insert_failed_items:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
return {
|
return {
|
||||||
'success': False,
|
'success': False,
|
||||||
'message': f'批量导入失败: {len(failed_items)}条记录处理失败',
|
'message': f'批量导入失败: {len(insert_failed_items)}条记录插入失败',
|
||||||
'total_count': total_count,
|
'total_count': total_count,
|
||||||
'success_count': success_count,
|
'success_count': success_count,
|
||||||
'failed_count': failed_count,
|
'failed_count': failed_count,
|
||||||
|
|||||||
@@ -354,12 +354,14 @@ class SectionDataService(BaseService[SectionData]):
|
|||||||
logger.error(f"Failed to insert batch: {str(e)}")
|
logger.error(f"Failed to insert batch: {str(e)}")
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
# 如果有失败记录,不提交事务
|
# 如果有插入失败记录(不是跳过记录),不提交事务
|
||||||
if failed_items:
|
# 跳过记录不应该影响事务,只插入失败的记录才需要回滚
|
||||||
|
insert_failed_items = [item for item in failed_items if '插入失败' in item.get('error', '')]
|
||||||
|
if insert_failed_items:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
return {
|
return {
|
||||||
'success': False,
|
'success': False,
|
||||||
'message': f'批量导入失败: {len(failed_items)}条记录处理失败',
|
'message': f'批量导入失败: {len(insert_failed_items)}条记录插入失败',
|
||||||
'total_count': total_count,
|
'total_count': total_count,
|
||||||
'success_count': success_count,
|
'success_count': success_count,
|
||||||
'failed_count': failed_count,
|
'failed_count': failed_count,
|
||||||
|
|||||||
@@ -487,12 +487,14 @@ class SettlementDataService(BaseService[SettlementData]):
|
|||||||
logger.error(f"Failed to insert batch: {str(e)}")
|
logger.error(f"Failed to insert batch: {str(e)}")
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
# 如果有失败记录,不提交事务
|
# 如果有插入失败记录(不是跳过记录),不提交事务
|
||||||
if failed_items:
|
# 跳过记录不应该影响事务,只插入失败的记录才需要回滚
|
||||||
|
insert_failed_items = [item for item in failed_items if '插入失败' in item.get('error', '')]
|
||||||
|
if insert_failed_items:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
return {
|
return {
|
||||||
'success': False,
|
'success': False,
|
||||||
'message': f'批量导入失败: {len(failed_items)}条记录处理失败',
|
'message': f'批量导入失败: {len(insert_failed_items)}条记录插入失败',
|
||||||
'total_count': total_count,
|
'total_count': total_count,
|
||||||
'success_count': success_count,
|
'success_count': success_count,
|
||||||
'failed_count': failed_count,
|
'failed_count': failed_count,
|
||||||
|
|||||||
Reference in New Issue
Block a user