事务 Rollback on 错误
错误信息
current 事务是 aborted, commands ignored 直到端 的事务块
快速摘要
earlier 语句 in 你的事务失败. 事务是 现在 in aborted 状态 and 将不 执行任何更多 commands 直到你 ROLLBACK.
为什么发生
语句 in 事务失败 (constraint violation, 语法错误, etc.)
application continued 发送 commands 之后失败不使用 rolling 后面
不检查 for 错误之后每个语句 in 事务
最小示例
✗ 错误代码
BEGIN;
INSERT INTO orders (id, user_id) VALUES (1, 999); -- fails: user 999 doesn't exist
UPDATE users SET last_order = NOW() WHERE id = 999; -- ignored: transaction is aborted
COMMIT;✓ 修复代码
BEGIN;
INSERT INTO orders (id, user_id) VALUES (1, 999); -- fails
ROLLBACK; -- roll back and start fresh
-- Fix the root cause, then retry in a new transaction之后首先语句失败, 你必须 ROLLBACK 之前运行任何更多 commands. Rolling 后面 clears aborted 状态.
如何诊断
检查错误那个 preceded 这个消息 — 那个是 真实 cause
Review 事务's 语句序列到 查找 failing 语句
Ensure 你的 application 检查 for 错误之后每个语句
如何修复
Roll 后面事务:
ROLLBACKFix 语句那个 caused 原始错误
开始新 事务 and retry
Use SAVEPOINTs 到部分地 recover 在内事务
如何预防
检查 for 错误之后每个语句 in 事务
Roll 后面 immediately 当语句失败
Use savepoints 如果你 需要到 recover 从部分 failures
Use 事务 wrapper 那个 auto-rolls-后面 on 错误