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})
posted @ 2019-07-31 11:13  robertzhou  阅读(459)  评论(0编辑  收藏  举报