Several ways to implement transactions in django

1. Three ways to implement transactions

1.1 Globally start the transaction ---> Globally start the transaction, which is bound to the whole process of http request response

DATABASES = {
         'default': {
              #全局开启事务,绑定的是http请求响应整个过程
             'ATOMIC_REQUESTS': True, 
         }
    }
from django.db import transaction
    # 局部禁用事务
@transaction.non_atomic_requests
def seckill(request):
   return HttpResponse('秒杀成功')

 1.2 A view function in a thing

 # fbv开启
    from django.db import transaction
    @transaction.atomic
    def seckill(request):
        return HttpResponse('秒杀成功')
# cbv开启
from django.db import transaction
from rest_framework.views import APIView
class SeckillAPIView(APIView):
     @transaction.atomic
     def post(self, request):
         pass

1.3 Partial use of transactions

from django.db import transaction
def seckill(request):
    with transaction.atomic():     #不用提交事务   with上下文管理器
        pass  # 都在一个事物中
    return HttpResponse('秒杀成功')

2 Rollback and savepoint of things

# 1 普通事务操作(手动操作)
transaction.atomic()  # 开启事务
transaction.commit()  # 提交事务
transaction.rollback() # 回滚事务

# 2 可以使用上下文管理器来控制(自动操作)
with transaction.atomic():  # 自动提交和回滚

save point

In transactional operations, we often explicitly set savepoints (savepoint).
Once an exception or error occurs, we use the savepoint_rollback method to allow the program to roll back to the specified savepoint.
If there is no problem, use the savepoint_commit method to commit the transaction.

from .models import Book
from django.db import transaction
def seckill(request):
    with transaction.atomic():
        # 设置回滚点,一定要开启事务
        sid = transaction.savepoint()
        print(sid)
        try:
            book = Book.objects.get(pk=1)
            book.name = '红楼梦'
            book.save()
        except Exception as e:
            # 如发生异常,回滚到指定地方
            transaction.savepoint_rollback(sid)
            print('出异常了,回滚')
        # 如果没有异常,显式地提交一次事务
        transaction.savepoint_commit(sid)
    return HttpResponse('秒杀成功')
transaction.atomic()  # 开启事务
sid = transaction.savepoint() # 设置保存点
transaction.savepoint_rollback(sid) # 回滚到保存点
transaction.savepoint_commit(sid) #提交保存点

3 After the transaction is committed, execute a callback function

 Sometimes we want to perform additional tasks immediately after the current transaction is submitted, such as sending an email to the seller immediately after the customer places an order

 Case number one

def send_email():
    print('发送邮件给卖家了')
def seckill(request):
    with transaction.atomic():
        # 设置回滚点,一定要开启事务
        sid = transaction.savepoint()
        print(sid)
        try:
            book = Book.objects.get(pk=1)
            book.count = book.count-1
            book.save()
        except Exception as e:
            # 如发生异常,回滚到指定地方
            transaction.savepoint_rollback(sid)
        else:
            transaction.savepoint_commit(sid)     #提交事务
            transaction.on_commit(send_email)     #提交事务之后执行send_email函数
            
    return HttpResponse('秒杀成功')

案例二
transaction.on_commit(lambda: send_sms.delay('1898288322'))
#异步提交,利用celery提交异步任务

Guess you like

Origin blog.csdn.net/xiaolisolovely/article/details/132301381