1
當與多個進程/工作人員與Gunicorn運行Django我遇到了一些手動MySQL數據庫事務的死鎖問題。Django數據庫事務和死鎖
DatabaseError(1205, 'Lock wait timeout exceeded; try restarting transaction')
我的設置使用多個數據庫,我的函數需要傳遞數據庫才能在調用它時使用。出於這個原因,我不能使用標準Django transaction decorators,因爲db需要被硬編碼爲參數。我檢查了裝飾代碼來看看事務是如何管理的,我的功能看起來像這樣:
from django.db import connections
def process(self, db, data):
# Takeover transaction management
connections[db].enter_transaction_management(True)
connections[db].managed(True)
# Process
try:
# do things with my_objects...
for obj in my_objects:
obj.save(using=db)
connections[db].commit()
except Exception as e:
connections[db].rollback()
finally:
connections[db].leave_transaction_management()
任何人能發現什麼可能是錯的怎麼回事?