title: Django transaction failure for nested atomic blocks and concurrent processes on MySQL
body: <p>I've faced following issue and I'm not entirely sure it's a django or MySQL issue. 
  It happens when I want to retry django's save method in case of database error such as deadlock. Lets not focus on how ugly it is because it was just temporary hack which revealed potentially another problem.</p>
  
  <p>To reproduce it I've prepared a script that runs 3 concurrent processes and simulate database failure by raising AssertionError.</p>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>from django.db import models, transaction
  from time import sleep
  from django.db.utils import OperationalError
  
  
  class ModelA(models.Model):
      name =  models.CharField(max_length=255)
  
      def __unicode__(self):
          return self.name
  
      def save(self, *args, **kwargs):
  
          def save_record(attempt):
              print attempt
              try:
                  with transaction.atomic():
                      super(ModelA, self).save(*args, **kwargs)
                      if attempt &gt; 1:
                          assert False
              except (AssertionError, OperationalError):
                  # dirty hack to retry
                  sleep(1)
                  if attempt &gt; 0:
                      save_record(attempt-1)
                  else:
                      raise
  
          save_record(5)
  </code></pre>
  
  <p><strong>test script</strong></p>
  
  <pre><code>import time
  from multiprocessing import Process
  
  from django.core.management.base import BaseCommand
  from django.db import transaction
  
  from atomic import models
  
  
  @transaction.atomic
  def create_record():
      a = models.ModelA()
      a.name = "test {}".format(time.time())
      a.save()
  
  
  class Command(BaseCommand):
  
      def handle(self, *args, **options):
          procs = []
          for i in range(3):
              p = Process(target=create_record)
              procs.append(p)
  
          for p in procs:
              p.start()
  
          for p in procs:
              p.join()
  </code></pre>
  
  <p>If I run only 1 process everything works but with 3 concurrent processes 1 works (saves data) and another 2 fails with following traceback</p>
  
  <pre><code>Traceback (most recent call last):
    File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
      self.run()
    File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
      self._target(*self._args, **self._kwargs)
    File "/home/blue/.virtualenvs/atomictest/local/lib/python2.7/site-packages/django/db/transaction.py", line 371, in inner
      return func(*args, **kwargs)
    File "/media/django/atomictest/atomic/management/commands/test_atomic.py", line 14, in create_record
      a.save()
    File "/media/django/atomictest/atomic/models.py", line 29, in save
      save_record(5)
    File "/media/django/atomictest/atomic/models.py", line 25, in save_record
      save_record(attempt-1)
    File "/media/django/atomictest/atomic/models.py", line 25, in save_record
      save_record(attempt-1)
    File "/media/django/atomictest/atomic/models.py", line 25, in save_record
      save_record(attempt-1)
    File "/media/django/atomictest/atomic/models.py", line 18, in save_record
      super(ModelA, self).save(*args, **kwargs)
    File "/home/blue/.virtualenvs/atomictest/local/lib/python2.7/site-packages/django/db/models/base.py", line 545, in save
      force_update=force_update, update_fields=update_fields)
    File "/home/blue/.virtualenvs/atomictest/local/lib/python2.7/site-packages/django/db/models/base.py", line 573, in save_base
      updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
    File "/home/blue/.virtualenvs/atomictest/local/lib/python2.7/site-packages/django/db/models/base.py", line 635, in _save_table
      forced_update)
    File "/home/blue/.virtualenvs/atomictest/local/lib/python2.7/site-packages/django/db/models/base.py", line 679, in _do_update
      return filtered._update(values) &gt; 0
    File "/home/blue/.virtualenvs/atomictest/local/lib/python2.7/site-packages/django/db/models/query.py", line 510, in _update
      return query.get_compiler(self.db).execute_sql(None)
    File "/home/blue/.virtualenvs/atomictest/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 980, in execute_sql
      cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
    File "/home/blue/.virtualenvs/atomictest/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
      cursor.execute(sql, params)
    File "/home/blue/.virtualenvs/atomictest/local/lib/python2.7/site-packages/django/db/backends/util.py", line 69, in execute
      return super(CursorDebugWrapper, self).execute(sql, params)
    File "/home/blue/.virtualenvs/atomictest/local/lib/python2.7/site-packages/django/db/backends/util.py", line 47, in execute
      self.db.validate_no_broken_transaction()
    File "/home/blue/.virtualenvs/atomictest/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 372, in validate_no_broken_transaction
      "An error occurred in the current transaction. You can't "
  TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
  </code></pre>
  
  <p>I use django 1.6.5 and MySQL 5.5.37. Tested also with sqlite and postgres and there's no such issue with those backends.
  I've also noticed that this happends only with nested atomic blocks. If i remove @transaction.atomic decorator from create_record() function it works again.</p>