通过阅读文档:
我知道
atomic blocks can be nested. In this case, when an inner block completes successfully, its effects can still be rolled back if an exception is raised in the outer block at a later point.
但是,我的问题是,对于如下代码结构:
@transaction.atomic
def A():
## something
B()
C()
## something
@t
我正在尝试更新django 1.8.4中的两个IntegerField,所以我决定使用原子事务,但我有一些疑问:
1-在这种情况下使用原子事务是个好主意吗?使用它的真正好处是什么?它的效率有多高?
2-我怎样才能检查这两件作品的工作原理是否相同?
一个。
@transaction.atomic
class LinkManager(models.Manager):
def vote_up(self, pk, increment=True):
if increment:
<update field 1, incrementing by 1>
Django的queryset.update方法似乎在transaction.atomic上下文管理器下执行。在update期间,我什么时候需要在代码中显式地执行它?或者,这样做有什么好处,或者不执行它会有什么问题?
代码
try:
queryset = Model.objects.filter(a=1)
if queryset.count():
with transaction.atomic():
queryset.update(a=2) # queryset will [] after this.
for it
我使用Django的命令执行一些涉及数据库操作的任务:
class SomeCommand(BaseCommand):
@transaction.atomic
def handle(self, *args, **options):
# Some stuff on the database
如果在执行我的程序时抛出异常,@transaction.atomic保证回滚。我可以强制这种行为而不抛出异常吗?类似于:
# Doing some stuff, changing objects
if some_condition:
# ABANDON ALL CHA
阅读本文档
这是on_commit的用例
with transaction.atomic(): # Outer atomic, start a new transaction
transaction.on_commit(foo)
# Do things...
with transaction.atomic(): # Inner atomic block, create a savepoint
transaction.on_commit(bar)
# Do more things...
# foo() and then bar()
当async def call_test(request):调用async def test():时,如下所示(我使用async def test():)
async def test():
for _ in range(0, 3):
print("Test")
async def call_test(request):
await test() # Here
return HttpResponse("Call_test")
在控制台上显示下面的正确结果没有错误:
Test
Test
Test
但是,当我将@tran
这是原子事务的适当使用吗?为什么或者为什么不? def save(self, **kwargs):
try:
with transaction.atomic:
super(User, self).save(**kwargs)
if self.image:
img = Image.open(self.image.path)
if img.height > 300 or img.width > 300:
out
我有PostgreSQL的Django 1.11应用程序。请看下面的代码。
那里有可能有比赛条件吗?恐怕我可以在diff=-account.hours上获得比赛条件。transaction.atomic是否从比赛状态中保存?
from django.db import transaction
def write_off(account_ids):
accounts = Account.objects.filter(id__in=account_ids)
for account in accounts:
with transaction.atomic():
我有一个ModelForm,我想在其中重写save()方法来创建相关的对象。我需要在transaction.atomic()中使用它
这样应用是正确的吗:
class MyForm(ModelForm):
@transaction.atomic
def save(self, commit):
self.instance.save()
# and create related objects using self.instance..
因为我总是看到它被应用在视图上,但是我的视图是基于类的,所以我不能应用这个装饰器。而且对我来说,将其应用于执行
如果我做了一个select_for_update,什么时候/如何释放这个锁?一些示例代码:
for rec_id in list(1,2,3):
record = MyModel.objects.select_for_update().get(pk=rec_id)
# Do several things to this record
record.save()
是在save()之后释放锁,还是在视图返回并完成整个事务之后释放锁?如何控制锁的粒度?
医生们似乎没有说:
我正在努力使嵌套的transaction.atomic()工作。当使用以下错误退出第一个transaction.atomic()时,以下代码块会崩溃
from django.contrib.auth.models import User
from django.test import TransactionTestCase
from django.db import transaction
class FooTest(TransactionTestCase):
def test_bar(self):
with transaction.atomic():
在视图中的transaction.atomic()中有一个代码块。我的问题是django是否在幕后创建了一些内置的表锁定。
with transaction.atomic():
#code block that does database operations
update_user() #this updates user table
create_customer_products() #this updates user id to customer products table
原因是我在运行代码块时出现了“锁定等待超时;尝试重新启动事务”错误。
设置是cent
我得到下面的堆栈无法弄清楚问题是什么。我知道这和我的URLS.pi有关,我正试着做一个美味的馅饼url来建立一个恢复服务
ImportError: importlib._bootstrap is not a frozen module
Traceback (most recent call last):
File "/usr/local/lib/python3.3/dist-packages/django/core/urlresolvers.py", line 339, in urlconf_module
return self._urlconf_module
At
我在transaction.atomic()中有两个DB调用
样本码
with transaction.atomic():
result1, created = SomeModel.objects.get_or_create(**somedata)
if not created:
flag = SomeOtherModel.objects.filter(somemodel=result1).exists()
if flag:
result1.some_attr = value1 if flag else value2
关于如何理解transaction.atomic(),我有一个小问题。如果我想通过这样的单个事务创建大容量对象
emails = [1, 2, 3, 4, 5]
for email in emails:
Users.objects.create(email=email)
1)上述代码在每次迭代中创建并执行一个查询。因此,每个电子邮件的日期时间字段都是不同的。这是正确的吗?
但是,如果我使用:
with transaction.atomic():
emails = [1, 2, 3, 4, 5]
for email in emails:
User
我试图通过使用select_for_update实用程序来利用django的行级锁定。根据文档,这只能在transaction.atomic块内部使用。使用transaction.atomic块的副作用是,如果我的代码抛出异常,所有数据库更改都会被回滚。我的用例是这样的,实际上我希望保留数据库更改,并允许异常传播。这给我留下了如下代码:
with transaction.atomic():
user = User.objects.select_for_update.get(id=1234)
try:
user.do_something()
except
我有下面的场景:
@transaction.atomic()
def some_method():
# some database logic
a.save() # Success. Eligible for Rollback.
with transaction.atomic():
# some more database logic
# Success. Shouldn't be rollbacked
b.save()
raise Exception
我想要做的是,当我提出异常时,内部事务组的更改不
我想在Django模型中插入一个基于模型的最后id的行。我的疑问是,
Order.objects.create(group=123131, identifier=friendly_invoice.encode(Order.objects.last().id))
# group and identifier are unique together
我记录了查询,如所示,
(0.016) SELECT `order`.`id`, `order`.`group`, `order`.`identifier`, `order`.`created`, `order`.`modified` FROM `or
当在transaction.atomic块内执行代码并引发异常时,数据库处理程序将被标记为需要回滚。如果仍在该transaction.atomic块内执行后续查询,将引发以下错误:
TransactionManagementError:当前事务中发生了错误。直到“原子”结束时才能执行查询
此时,实际的根错误是模糊的,很难访问,因此需要跳转到Django的事务代码中。
一个可能导致此错误的简单示例:
def someview(request):
with transaction.atomic():
// do some things
instance