In general, locks are prevented by accessing objects (tables, pages, rows) always in the same order. In your example, there is one process that refers to forum
first and forumThread
second, and the other is the other way around. update
usually searches first for rows to update and uses S-locks during the search. The lines that he defined for the change are blocked by X-locks, and then the actual change occurs.
Quick and dirty decisions can be to make begin Tran
and then lock
objects in the correct order and perform an update, followed by a commit
that will release the locks again. But this will lead to a decrease in the total volume of your site due to blocking locks.
It is best to identify two statements (you can edit your question and give us another when you find it) and a plan of implementation. It should be possible to rewrite transactions one way or another in order to access all objects in the same order - and prevent a deadlock.
source share