Wednesday, June 27, 2012

Django DB transactions and deadlocks with multiple WSGI processes

When running Django with Gunicorn with multiple processes/workers I'm getting a deadlock issue with some of my manual MySQL database transactions.

    DatabaseError(1205, 'Lock wait timeout exceeded; try restarting transaction')

My setup uses multiple databases, and my function needs to be passed the database to use when it's called. For this reason, I can't use the standard [Django transaction decorators](https://docs.djangoproject.com/en/1.4/topics/db/transactions/) as the db needs to be hard-coded as an argument. I've inspected the decorator code to look at how transactions are managed, and my function looks like this:

    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()

Can anyone spot what may be going wrong here?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/o4j00SlcSNwJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment