Note: The transaction (process ID 56) and another process are deadlocked. | the communication buffer zone resource has been selected as the deadlock victim ., Note as dead

Source: Internet
Author: User

Note: The transaction (process ID 56) and another process are deadlocked. | the communication buffer zone resource has been selected as the deadlock victim ., Note as dead

An exception occurred during SQL concurrent UPDATE today: (the Code is as follows)

// Parallel class can generate concurrent operations (that is, multithreading) Parallel. forEach (topics, topic =>{// DBHelper is an encapsulated database operation class. The following line of code will execute the UPDATE statement DBHelper. update (topic, "TopicID =" + topic. topicID );});

This problem occurs because an SQL statement is a transaction by default in SQLServer. When multiple threads are updated simultaneously, multiple transactions are generated at the same time. Transaction A waits for transaction B to end, and transaction B waits for transaction A to end, resulting in A deadlock.

Solution: lock (that is, when multiple threads access the lock code area at the same time, only one thread is allowed to enter, and other threads are waiting)

// Declare the static read-only Lock object private static readonly object o = new object (); Parallel. forEach (topics, topic =>{// lock (o) {// At the same time, only one thread can enter DBHelper. update (topic, "TopicID =" + topic. topicID );}});

Related Article

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.