1 def user_atomic(): 2 User.objects.create(name=‘purk1‘, email=‘[email protected]‘) 3 User.objects.create(name=‘purk3‘, email=‘[email protected]‘) 4 try: 5 User.objects.create(user_id=‘purk2‘, name=‘purk2‘, email=‘[email protected]‘) 6 except Exception as e: 7 e 8 9 10 @transaction.atomic 11 def user_atomic_decorator(): 12 user = User.objects.create(name=‘purk1‘, email=‘[email protected]‘) 13 User.objects.create(name=‘purk3‘, email=‘[email protected]‘) 14 try: 15 User.objects.create(user_id=‘purk2‘, name=‘purk2‘, email=‘[email protected]‘) 16 except Exception as e: 17 e
user_id 为uuid类型,所以try里面肯定会抛异常
result:
用atomic装饰之后,里面的结果就被回滚了
时间: 2024-11-04 11:02:28