mysql 重试_重试MySQL/SQLAlchemy的死锁
我已經找了很長時間,找不到解決我問題的辦法。我們將SQLAlchemy與MySQL一起用于我們的項目,我們多次遇到可怕的錯誤:1213, 'Deadlock found when trying to get lock; try restarting transaction'.
在這種情況下,我們最多嘗試重新啟動事務三次。
我已經開始編寫一個decorator來實現這一點,但是我不知道如何在失敗之前保存會話狀態,然后在失敗之后重試相同的事務?(因為每當引發異常時,SQLAlchemy都需要回滾)
到目前為止我的工作def retry_on_deadlock_decorator(func):
lock_messages_error = ['Deadlock found', 'Lock wait timeout exceeded']
@wraps(func)
def wrapper(*args, **kwargs):
attempt_count = 0
while attempt_count < settings.MAXIMUM_RETRY_ON_DEADLOCK:
try:
return func(*args, **kwargs)
except OperationalError as e:
if any(msg in e.message for msg in lock_messages_error) \
and attempt_count <= settings.MAXIMUM_RETRY_ON_DEADLOCK:
logger.error('Deadlock detected. Trying sql transaction once more. Attempts count: %s'
% (attempt_count + 1))
else:
raise
attempt_count += 1
return wrapper
總結
以上是生活随笔為你收集整理的mysql 重试_重试MySQL/SQLAlchemy的死锁的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “华为擎云”品牌发布 华为终端商用市场驶
- 下一篇: 问界汽车将升级HarmonyOS 3 带