orm 高併發保證數據一致性

python2python

1.with transaction.commit_on_success()開啓事務數據庫

2.使用select_for_update來告訴數據庫鎖定對象,直到事務完成code

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})
相關文章
相關標籤/搜索