gpt4 book ai didi

python - TransactionManagementError - 当 'atomic' block 处于事件状态时,这是被禁止的

转载 作者:太空狗 更新时间:2023-10-29 21:46:18 29 4
gpt4 key购买 nike

将我的项目从 django 1.5.1 升级到 1.6.5 时,我遇到了这个奇怪的问题。

This is forbidden when an 'atomic' block is active.

我知道 Database Transaction changes for django 1.6并相应地更改了设置。大多数情况下都有效,除非访问 request.user 对象。

代码,例如:

with transaction.atomic():
if hasattr(request, 'user') and getattr(request.user, 'id', None):
#blah

这是堆栈跟踪:

Environment:


Request Method: GET
Request URL: <domain>/api/v1/browser_id/

Django Version: 1.6.5
Python Version: 2.7.3

<Installed Applications & Middlewares snipped for brevity>

Traceback:
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
371. return func(*args, **kwargs)
File "/home/kravindra/workspace/puppysite/puppy/kennel/views/etag_session.py" in browser_id
43. if hasattr(request, 'user') and getattr(request.user, 'id', None):
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/utils/functional.py" in inner
213. self._setup()
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/utils/functional.py" in _setup
298. self._wrapped = self._setupfunc()
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/auth/middleware.py" in <lambda>
18. request.user = SimpleLazyObject(lambda: get_user(request))
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/auth/middleware.py" in get_user
10. request._cached_user = auth.get_user(request)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in get_user
140. user_id = request.session[SESSION_KEY]
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in __getitem__
47. return self._session[key]
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in _get_session
173. self._session_cache = self.load()
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/cached_db.py" in load
52. self.create()
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py" in create
40. self.save(must_create=True)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/cached_db.py" in save
62. super(SessionStore, self).save(must_create)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py" in save
63. obj.save(force_insert=must_create, using=using)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/models/base.py" in save
545. force_update=force_update, update_fields=update_fields)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
582. update_fields=update_fields, raw=raw, using=using)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py" in send
185. response = receiver(signal=self, sender=sender, **named)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
430. with self:
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/transaction.py" in __enter__
422. self.entering(self.using)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/transaction.py" in entering
483. enter_transaction_management(using=using)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/transaction.py" in enter_transaction_management
70. get_connection(using).enter_transaction_management(managed, forced)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/backends/__init__.py" in enter_transaction_management
287. self.validate_no_atomic_block()
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/backends/__init__.py" in validate_no_atomic_block
367. "This is forbidden when an 'atomic' block is active.")

Exception Type: TransactionManagementError at /api/v1/browser_id/
Exception Value: This is forbidden when an 'atomic' block is active.

查看堆栈跟踪,

/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/cached_db.py in load
self.create() ...
▼ Local vars
Variable Value
e
DoesNotExist('Session matching query does not exist.',)
self
<django.contrib.sessions.backends.cached_db.SessionStore object at 0x7f61d401c6d0>
data
None

SessionStore 引发异常。

使用 POSTGRES 作为后端数据库。后端 session 存储相关设置为:

SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db' #which is persistent storage

ATOMIC_REQUESTS = True

关于如何解决这个问题的任何指示?

我发现有很多关于 SO 的问题,以及一般解决 TransactionManagementError 问题的博文,但没有专门解决这个问题

最佳答案

从回溯看来,您的 post_save 信号接收器之一仍在使用以下已弃用的 transaction API 之一:

  • transaction.Transaction
  • transaction.autocommit
  • transaction.commit_on_success
  • transaction.commit_manually
  • commit_on_success_unless_managed

确保将其替换为 transaction.atomic 实例,这样您的问题就会消失。

如果您在找到罪魁祸首接收器时遇到问题,请告诉我。

关于python - TransactionManagementError - 当 'atomic' block 处于事件状态时,这是被禁止的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25370467/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com