def user_atomic():
User.objects.create(name='purk1', email='[email protected]') User.objects.create(user_id='purk2', name='purk2', email='[email protected]') User.objects.create(name='purk3', email='[email protected]') @transaction.atomic
def user_atomic_decorator():
User.objects.create(name='purk1', email='[email protected]') User.objects.create(user_id='purk2', name='purk2', email='[email protected]') User.objects.create(name='purk3', email='[email protected]') @transaction.atomic
def user_atomic_decorator_1():
User.objects.create(name='purk1', email='[email protected]') try:
with transaction.atomic():
User.objects.create(user_id='purk2', name='purk2', email='[email protected]')
except Exception as e:
e
User.objects.create(name='purk3', email='[email protected]') @transaction.atomic
def user_atomic_decorator_except():
User.objects.create(name='purk1', email='[email protected]')
try:
User.objects.create(user_id='purk2', name='purk2', email='[email protected]')
except Exception as e:
e
User.objects.create(name='purk3', email='[email protected]')
atomic
user_id 为uuid类型,所以user_id='purk2'肯定会出错,
result:
在atomic中错误的使用try except则会抛出TransactionManagementError的异常,同时transaction失效了,没有正常回滚。
用atomic装饰之后,里面的结果就被回滚了,但是用try except包裹的atomic不会影响外层的transaction回滚