title: Django TransactionManagementError
body: <p>In my application I have cause to refresh my django database connection in case it has dropped during a long blocking (not database related) task.</p>
  <p>This generally works but I have found that it fails when running as a TestCase.  My code:</p>
  <p>class Run_TC(TestCase):
  def setUp(self):
  pass</p>
  <pre><code>#
  
  def test(self):
      from django.db import connection
      from django.db.utils import OperationalError
      import time
  
      connection.close()
  
      db_conn = False
      while not db_conn:
          try:
              connection.ensure_connection()
              db_conn = True
          except OperationalError:
              print('Database unavailable, waiting 1 second...')
              time.sleep(1)
  
      print('Database available')
  
      proc_info = models.ProcessingInfo()
      proc_info.processing_name = &quot;xxx&quot;
      
      proc_info.in_progress = False
  
      proc_info.save()
  
  
  if __name__ == '__main__':
      suite = unittest.TestLoader().loadTestsFromTestCase(Run_TC)
      unittest.TextTestRunner().run(suite)
  </code></pre>
  <p>This gives the following error:</p>
  <pre><code>File &quot;/usr/local/lib/python3.7/dist-packages/django/db/backends/base/base.py&quot;, line 448, in validate_no_broken_transaction
      &quot;An error occurred in the current transaction. You can't &quot;
  django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
  </code></pre>
  <p>If I do not call connection.close() and then ensure_connection(), the creation of the model works fine.</p>
  <p>Any idea what the issue could be?</p>