Note: The transaction (Process ID 56) is deadlocked with another process in the lock | The communication buffer resource and has been selected as the deadlock victim.

Source: Internet
Author: User

An exception was encountered today when doing SQL Concurrency update: (Code below)

// Parallel class can produce concurrent operations (i.e. multithreading) Parallel.ForEach (topics, topic ={    //DBHelper is a encapsulated database operation class, the following line of code executes the UPDATE statement    "topicid=" + topic. TOPICID);});

This problem occurs because, in SQL Server by default, an SQL statement is a transaction. When multi-threaded simultaneous update, it will produce multiple transactions at the same time, a transaction waits for the end of B transaction, b transaction waits for a transaction to end, then causes the deadlock.

Workaround: Lock locking (that is, when multiple threads access the lock code area at the same time, only one thread is allowed to enter, and the other threads are in a wait state)

 //  declares a static read-only lock object   static  readonly  object  o = new  object  (); Parallel.ForEach (topics, topic  =>{ / /  lock  lock   (o) { //  Only one thread at a time can enter  Dbhelper.update (topic,  " topicid=   " + topic.    TOPICID); }});

Small note: The transaction (Process ID 56) is deadlocked with another process on the lock | Communication buffer resource and has been selected as the deadlock victim.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.