orm 高并发保证数据一致性

python2

1.with transaction.commit_on_success()开启事务

2.使用select_for_update来告诉数据库锁定对象,直到事务完成

with transaction.commit_on_success():
    ActivityJoinner.objects.create(activity=activity, sign_type=sign_type,
                                   user=request.user, center=center)
    info = ActivityInfo.objects.select_for_update().get(activity=activity)
    if sign_type == 1:
        info.joinner_sign_num += 1
   else:
        info.volunteer_sign_num += 1
        info.save()
        return Response({"status": res, "data": msg})

猜你喜欢

转载自www.cnblogs.com/robert-zhou/p/11275041.html