首页 > 其他 > 详细

select..for update

时间:2016-03-30 12:27:35      阅读:237      评论:0      收藏:0      [点我收藏+]

select..for update; 给数据库表手动上锁

   这条语句会开启一个session,直到这个session Commit,其他session才能执行更新、插入、删除操作,对查询没有影响,但是这张表再不能开启其他select..for update;

   使用情况:使用count(*)作为流水号字段的值时,在高并发情况下容易出现重复,此时用select..for update;执行插入前锁住这张表来保证流水号不重复,使用时把select..for update;与执行insert、update、delete 的语句放在service(业务层)下一个方法中,如果放在service下的两个以上方法中,controller类调用一个方法结束后会自动执行下commit,达不到锁住表的效果。

this.DaoBase.ExecuteCommand("select * from enrollment_student_info for update");//先开启select..for update把表锁上,后面过来的事物只能先等这个事物执行完了再执行
object res = this.DaoBase.ExecuteScalar("select count(*) from enrollment_student_info");
this.DaoBase.ExecuteNonquery("Insert...");

 

select..for update

原文:http://www.cnblogs.com/xbblogs/p/5336578.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!