Now, first, execute the Transaction 1 code and then immediately execute the Transaction 2 code. UPDATE TableA SET Name = 'Anurag From Transaction2' WHERE Id = 101 UPDATE TableB SET Name = 'Priyanka From Transaction2' WHERE Id = 1001 Then intentionally we delayed the transaction execution for 15 seconds by using WAITFOR DELAY ’00:00:15′ statement and then we try to execute the second update statement i.e. The transaction will start its execution and will execute the first update statement i.e. In between the first and second update statements, we have delayed the transaction execution for 15 seconds. UPDATE TableB SET Name = 'Priyanka From Transaction1' WHERE Id = 1001Īs you can see in the below code, here we have also written the transaction with two update statements. ![]() UPDATE TableA SET Name = 'Anurag From Transaction1' WHERE Id = 101 In between the first and second update statements, we have delayed the execution for 15 seconds. In such cases, neither of the transactions (processes) can move forward resulting in a deadlock.Īs you can see in the below code, here we have written the transaction with two update statements. At the same time, Process B needs the resource Table A which is already locked by Process A. Now to complete its execution, process A needs the resource Table B which is already locked by Process B. Process A started and it acquires a lock on table A and in the meantime, Process B started and it acquired a lock on Table B. We have two tables such as Table A and Table B in the database. when a deadlock occurs in a database.Īs shown in the above diagram, we have two processes (process means transactions) such as Process A and Process B. If you are confused, then just have a look at the following diagram which explains the above points i.e. ![]() In such cases, neither of the processes can move forward, as each process is waiting for the other process to release the lock resulting in a deadlock in SQL Server. Data Structures and Algorithms TutorialsĪ deadlock occurs in a database when two or more processes have already a resource locked, and then each process wants to acquire a lock on the resource that the other process has already locked.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |