我有一个需要手动事务管理的视图函数,但是当我应用@transaction.commit_manually
装饰器时,Django总是引发下面的异常。
从下面的代码跟踪中可以看到,事务是在从视图返回之前提交的。
我在Windows和Linux上使用sqlite,使用django 1.4。
下面是django_跟踪的输出,后面是异常。要清楚:无论我是否使用django_跟踪,都会发生这种情况,并且当没有修饰符时,不会引发任何异常。这不是由“吞咽”异常引起的。
请注意,下面的第60行位于上下文处理器内部,因此位于包装视图的外部。
01->mainapp.views:1321: transaction.commit()
01->mainapp.views:1322: return render_to_response('mainapp/templates/incorporate.html',
01->mainapp.views:1323: RequestContext(request, form_params))
02-->mainapp.views:60: transaction.rollback_unless_managed()
02-->mainapp.views:61: return {'home_login_form': AuthenticationForm(request)}
Traceback (most recent call last):
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\contrib\staticfiles\handlers.py", line 67, in __call__
return self.application(environ, start_response)
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\contrib\staticfiles\handlers.py", line 67, in __call__
return self.application(environ, start_response)
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\core\handlers\wsgi.py", line 241, in __call__
response = self.get_response(request)
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\core\handlers\base.py", line 179, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\core\handlers\base.py", line 221, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\core\handlers\base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Marcin\Documents\oneclickcos\oneclickcos\mainapp\decorators.py", line 26, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\db\transaction.py", line 209, in inner
return func(*args, **kwargs)
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\db\transaction.py", line 203, in __exit__
self.exiting(exc_value, self.using)
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\db\transaction.py", line 288, in exiting
leave_transaction_management(using=using)
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\db\transaction.py", line 52, in leave_transaction_management
connection.leave_transaction_management()
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\db\backends\__init__.py", line 119, in leave_transaction_management
raise TransactionManagementError("Transaction managed block ended with "
TransactionManagementError: Transaction managed block ended with pending COMMIT/ROLLBACK
为了清楚起见,我已经检查了关于这个主题的其他问题,它们没有解决我的问题的方法。
最佳答案
事实证明,在模板呈现期间,存在数据库访问,因此通常的模式如下:
return render_to_response('mainapp/templates/incorporate.html',
RequestContext(request, form_params))
是问题的起因。我需要用以下内容来代替:
retval = render_to_response('mainapp/templates/incorporate.html',
RequestContext(request, form_params))
transaction.commit()
return retval
另外,其他的SO答案揭示了事务管理修饰符隐藏了所有的异常,而引发了事务管理异常。不幸的是,诊断这种情况的最简单方法是在不使用修饰器的情况下运行,并查看是否发生了异常,或者在try/except中包装整个视图。