SQLServer 并发问题 先select再update

child_1986 child_1986 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
SQLServer 并发问题

例如

并发执行以下两个事务

事务1

begin tran
declare @count int =0
select @count=[Count] from test4 where id=1
select @count as count1
waitfor delay '00:00:30' --等待30秒
update test4 set [Count]=@count+1 where id=1
commit tran

事务2

begin tran
declare @count int =0
select @count=[Count] from test4 where id=1 --holdlock人为加锁
select @count as count2
update test4 set [Count]=@count+1 where id=1
commit tran

要求:事务2的count2是事务1中已经update 加1后的数据

怎么在数据库层面解决

试过锁,总是出现死锁的问题
...全文
评论 5
收藏 已收藏
分享
返回顶部
child_1986
疑难问题 22,299 成员 121,727 内容