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