简体   繁体   English

在ac#项目中同时锁定和使用SQL Server 2008表?

[英]Simultaneous lock and use of SQL Server 2008 table in a c# project?

I have a SQL Server table and this table will be updated by a batch job every 5 minutes using backgroundworker multithread calls. 我有一个SQL Server表,并且此表将使用backgroundworker多线程调用每5分钟通过批处理作业进行更新。 Also i am using thread lock when i am inserting the table via batch job. 当我通过批处理作业插入表时,我也在使用线程锁定。 The same data can be accessed by the user of the application simultaneously. 应用程序的用户可以同时访问相同的数据。 I have my business logic in C#. 我在C#中有业务逻辑。 What is the best and optimized solution for this? 最佳和最佳的解决方案是什么? Can i use thread lock on this situation or not? 我可以在这种情况下使用线程锁定吗?

What's the problem you have (or that you anticipate)? 您有什么问题(或您预期的问题)?

SQL Server is made and optimized for handling lots of concurrent connections and users updating, inserting, reading data. SQL Server是为处理大量并发连接以及用户更新,插入和读取数据而制作和优化的 Just let it handle the work! 任其处理工作!

When your background worker thread updates the table, it will take exclusive (X) locks on those rows that it updates - but only on those rows (as long as you don't update more than 5000 rows at once). 当后台工作线程更新表时,它将对它更新的行进行排他(X)锁 -但仅对这些行(只要一次更新的行数不超过5000行)即可。

During that time, any other row in the table can be read - no problem at all, no deadlock in sight.... 在此期间,可以读取表中的任何其他行-完全没有问题,看不到死锁。

The problem could occur if you update more than 5000 rows in a single transaction - then SQL Server will do a Lock escalation to avoid having to keep track of too many locks, and it will lock the entire table with an (X) lock. 如果您在单个事务中更新5000多个行,则可能会发生问题-SQL Server将执行锁升级,以避免必须跟踪过多的锁,并且它将使用(X)锁来锁定整个表。 Until the end of that update transaction - no reads are possible anymore - but those are normal transactional locks - NOT deadlocks. 在该更新事务结束之前-不再可能进行读取-但这些是正常的事务锁- 不能死锁。

So where is your problem / your issue? 那么您的问题在哪里?

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

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