防止在SQL中并发插入 [英] Prevent concurrent inserts in sql

查看:67
本文介绍了防止在SQL中并发插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个tblStudents表,其中包含有关student_courses的信息.根据课程类型和位置类型,为每个新学生分配一门课程的CourseNo(varchar).例如:

There is a table tblStudents which contain information about student_courses. Every new student is assigned a CourseNo (varchar)for a course depending on course type and location type. eg:

 CourseNo              Course       Location    StudentID   Date
NET_MUMBAI_001      Net     Mumbai       100        2/6/2015
NET_MUMBAI_002      Net     Mumbai       101        2/6/2015
NET_DELHI_001       Net     Delhi        100        2/6/2015
JAVA_DELHI_001      Java        Delhi        101        2/6/2015
JAVA_DELHI_002      Java        Delhi        102        2/6/2015
JAVA_DELHI_003      Java        Delhi        122        2/6/2015
JAVA_DELHI_004      Java        Delhi        112        2/6/2015

在招收新学生时,它将检查课程和位置的组合的最近使用率最高的号码(在这种情况下,孟买和Net为002),并生成新的课程号.(NET_MUMBAI_003).当有并发插入时,将生成的编号重复.怎样才能防止这种情况发生?

When a new student is being enrolled, it checks for the last highest used number (002 in this case for Mumbai and Net) for that combination of course and location and generates a new course no. (NET_MUMBAI_003). When there are concurrent inserts, the number being generated is duplicated. How can the same be prevented?

推荐答案

感谢您的输入.

为了达到相同目的,同步使用了以下两项功能.1.应用唯一索引2.使用的隔离级别可通过更新锁序列化

Used the following 2 things in sync for achieving the same. 1. Applied unique index 2. Used isolazation level as serializable with update lock

这篇关于防止在SQL中并发插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆