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 );}});