Concurrent Operations: When more than one user(s) / application(s) attempt operation (select / insert / update) on same data row(s), this is called Concurrent operation.
Concurrent operations may cause below given issues -
1. Dirty Reads: If session 2 executed during the 15 second delay. Session 2 will get updated data by session 1, but then session 1 get rolled back, so session 2 got wrong data its called Dirty Read.
Session 1
2. Unrepeatable Reads: If all these session executing parallel then 4 select statements of session 1 will get different values every time.
Session 1
3. Phantom Rows: User looking to set 20 for all active records. Session 2 is running parallel added new active values. These new rows are Phantom Rows.
Session 1
4. Lost Update: user is looking to set region id to 200, but parallel session 2 set it to 100. so user 1 lost his updates.
--------------------------------------------------------------------------
Solutions for these problems
Concurrent operations may cause below given issues -
1. Dirty Reads: If session 2 executed during the 15 second delay. Session 2 will get updated data by session 1, but then session 1 get rolled back, so session 2 got wrong data its called Dirty Read.
Session 1
Begin tran
update tb_city set regionid = 2 where id between 11 and 20
waitfor delay '00:00:15'
--Commit
Rollback
Session 2
select * from tb_city where id = 15
select * from tb_city where id between 100 and 200
select * from tb_city where id between 100 and 200
select * from tb_city where id between 100 and 200
select * from tb_city where id between 100 and 200
Session 2 update
tb_city set regionid = 2 where id between 100 and 200
Session 3 update
tb_city set regionid = 4 where id between 100 and 200
Session 4 update
tb_city set regionid = 9 where id between 100 and 200
Session 5 update
tb_city set regionid = 3 where id between 100 and 200
update tb_city set regionid = 20 where IsActive = 1
Session 2
insert into tb_city values (201, 1)
insert into tb_city values (202, 1)
insert into tb_city values (203, 1)
Session 1
update tb_city set regionid = 200 where IsActive = 1
Session 2
update tb_city set regionid = 100 where IsActive = 1
- Optimistic Lock
- Pessimistic Lock







