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()
任何人能发现什么可能是错的怎么回事?