Django和Postgres事务回滚

Dmi*_*erg 5 python django postgresql transactions

我有一段代码在后台进程中工作,看起来像

from django.db import transaction

try:

    <some code>

    transaction.commit()

except Exception, e:

    print e

    transaction.rollback()
Run Code Online (Sandbox Code Playgroud)

在测试中,我打破<some_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
Run Code Online (Sandbox Code Playgroud)

这就是我的期望.不好的是,当我在transaction.rollback调用后尝试访问数据库时,仍然会遇到相同的错误.如何成功回滚事务并使连接再次可用,我该怎么办?

顺便说一句,我也尝试插入print connection.queries调试代码,它总是返回一个空列表.是不是Django正在使用其他数据库连接?

代码在请求 - 响应周期之外运行.我尝试打开和关闭TransactionMiddleware,但它没有产生任何影响.

我正在使用Django 1.1和Postgres 8.4.

Dmi*_*nko 6

默认的TestCase对事务没有任何了解,在这种情况下需要使用TransactionalTestCase.