duanjia4969 2016-01-22 06:48
浏览 57

在事务中重复插入

I have scenario in which I got multiple records inserted into the Table even the checks are implemented, I explain the flow of the code below.

BEGIN Transection;
var model = select id from table1 where outlet_code = 123 and survey_id = 5
if(model){
     throw new Exception("Outlet is already Surveyed.");
}else{
     INSERT Record into the table
} 
Commit Transection;

Problem: I am getting multiple requests from the mobile side to upload survey, but the problem occur when I got multiple request with same outlet code.

First  Request outlet_code = 123abc and survey_id = 5
Second Request outlet_code = 123abc and survey_id = 5

Point is that Second request should not be inserted in the code however in some situations both records get inserted into the "table1"

I tested this scenario, but I got the exception which is fine, but there are records in the Database which violates this condition and I got both records in the database

kindly suggest what I am doing wrong,I just have an idea that it might be the problem of transaction committing order but I don't know how to solve this situation.

Database is SQL server 2008.

  • 写回答

1条回答 默认 最新

  • douyang5943 2016-01-22 07:48
    关注

    The easiest and cleanest way would be creating a primary key on ID columnn so table won't accept any inserts with duplicating IDs. This will throw an error though on a second insert so it should be handled as well.

    评论

报告相同问题?

悬赏问题

  • ¥20 C# TCP服务端,客户端退出后,不断有数据进来
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?