简体   繁体   English

SQL Server跨事务中的锁

[英]Locks in SQL Server Across Transactions

Here is my scenario why I need a row lock across transactions.. 这是我的情况,为什么我需要跨事务的行锁。

  • change the columns value to 5 (in SQL Server) 将列值更改为5(在SQL Server中)
  • change the columns value to 5 (in another resource, this can be a file or etc.) 将column的值更改为5(在另一个资源中,它可以是文件等)

Of course it's the case when everything is gone well. 当然,一切都进行得很好。 but if any problem occurs while doing the second change operation, I need to rollback the first change. 但是,如果在执行第二次更改操作时发生任何问题,我需要回滚第一个更改。 And also while doing the second change, nobody should be allowed to read or to write this row in SQL Server. 同样,在进行第二次更改时,不应允许任何人在SQL Server中读取或写入此行。

So I need to do that 所以我需要这样做

  • lock the column 锁定列
  • change the columns value to 5 (in SQL Server) 将列值更改为5(在SQL Server中)
  • change the columns value to 5 (in another resource) 将column的值更改为5(在另一个资源中)
  • if the above change is successfully done commit the column else rollback the column 如果以上更改成功完成,则提交该列,否则回滚该列
  • unlock the column 解锁专栏

And I also need something for the murphy case. 我还需要一些用于墨菲案的东西。 If I cannot reach the database after locking the row (in order to unlock or to rollback), it should be unlocked in a few seconds. 如果在锁定行后无法访问数据库(为了解锁或回滚),则应在几秒钟内将其解锁。

Is it possible to have something to do that in SQL Server or what ? 在SQL Server中可以做些什么吗?

Read up on distributed transactions and a compensating ressource manager. 阅读分布式事务和补偿性资源管理器。 THen you realize you can do all that in ONE transaction, managed by your transaction coordinator. 这样,您就可以在由事务协调员管理的一个事务中完成所有这些工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM