title: Django and Postgres transaction rollback
body: <p>I have a piece of code that works in a background process which looks like</p>
  
  <pre><code>from django.db import transaction
  
  try:
  
      &lt;some code&gt;
  
      transaction.commit()
  
  except Exception, e:
  
      print e
  
      transaction.rollback()
  </code></pre>
  
  <p>In a test, I break <code>&lt;some_code&gt;</code> with data that causes a database error. The exception is following</p>
  
  <pre><code>File "/home/commando/Development/Diploma/streaminatr/stream/testcases/feeds.py", line 261, in testInterrupt
  
      form.save(self.user1)                                                                                    
  
  File "/usr/lib/pymodules/python2.5/django/db/transaction.py", line 223, in _autocommit                     
  
      return func(*args, **kw)                                                                                 
  
  File "/home/commando/Development/Diploma/streaminatr/stream/forms.py", line 99, in save                    
  
      print(models.FeedChannel.objects.all())                                                                  
  
  File "/usr/lib/pymodules/python2.5/django/db/models/query.py", line 68, in `__repr__ `                       
  
      data = list(self[:REPR_OUTPUT_SIZE + 1])                                                                 
  
  File "/usr/lib/pymodules/python2.5/django/db/models/query.py", line 83, in `__len__ `                        
  
      self._result_cache.extend(list(self._iter))                                                              
  
  File "/usr/lib/pymodules/python2.5/django/db/models/query.py", line 238, in iterator                       
  
      for row in self.query.results_iter():                                                                    
  
  File "/usr/lib/pymodules/python2.5/django/db/models/sql/query.py", line 287, in results_iter               
  
      for rows in self.execute_sql(MULTI):                                                                     
  
  File "/usr/lib/pymodules/python2.5/django/db/models/sql/query.py", line 2369, in execute_sql               
  
      cursor.execute(sql, params)                                                                              
  
  InternalError: current transaction is aborted, commands ignored until end of transaction block
  </code></pre>
  
  <p>This is what I expect. The bad thing is that I still get the same error when I try to access the DB after <code>transaction.rollback</code> is called. What should I do to rollback the transaction succcessfully and make the connection usable once again?</p>
  
  <p>Btw, I also tried inserting <code>print connection.queries</code> to debug the code, and it always returns an empty list. Could it be that Django is using some other DB connection?</p>
  
  <p>The code is run outside of request-response cycle. I tried switching TransactionMiddleware on and off, but it gave no effect.</p>
  
  <p>I am using Django 1.1 and Postgres 8.4.</p>