每个都有平行的批量插入 [英] Bulk Insert with Parallel for each

查看:68
本文介绍了每个都有平行的批量插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我面临以下错误事务(进程ID 133)在锁定|通信缓冲区资源上与另一个进程发生了死锁选择作为僵局受害者。重新运行交易。



情景:

我正在使用 并行每个 批量插入 同时一些其他线程尝试从同一个表中读取数据并且我死了锁定错误。



请让我知道如何解决它。

Hi,

I am facing following error "Transaction (Process ID 133) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction."

Scenario :
I am using "parallel for each" to do "bulk insert during same time some other thread tries to read data from the same table and i get dead lock error.

Please let me know how to solve it.

推荐答案

其中一个可能的变体是在事务范围中执行每个操作,例如:



Hi, one of the possible variants is to execute each action in Transaction scope, for example:

Parallel.ForEach(MyIEnumerableSource, insertAction);

static void insertAction(EntityItem item)
{
using (TransactionScope scope = new TransactionScope(
     Required, new TransactionOptions() {IsolationLevel = ReadCommitted))
  {   
try{
      ExecuteInsertQuery(item);
    scope.Complete ();
}
catch{scope.Rollback();}
}


hi,



a)sql server中的select语句锁定表以避免在sql语句中使用(nolock)死锁问题。



b)如果表中没有索引,则delete语句会锁定整个表以避免这样创建索引。


a) Select statement in sql server locks the table to avoid deadlock issue use with(nolock) in sql statement.

b) If no index is present on table then delete statement locks the whole table to avoid this create an index.


这篇关于每个都有平行的批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆