About Me

My photo
Mumbai, Maharastra, India
He has more than 7.6 years of experience in the software development. He has spent most of the times in web/desktop application development. He has sound knowledge in various database concepts. You can reach him at viki.keshari@gmail.com https://www.linkedin.com/in/vikrammahapatra/ https://twitter.com/VikramMahapatra http://www.facebook.com/viki.keshari

Search This Blog

Showing posts with label WAITFOR. Show all posts
Showing posts with label WAITFOR. Show all posts

Saturday, September 7, 2019

What is Repeatable Read problem and how to solve it?

Let’s replicate the problem, for which I am creating a table StudentMarks and insert three records
create table studentMarks (
       id int
       ,name varchar(50)
       ,total_marks int
       )
    insert into studentMarks(id,name,total_marks) values( 1,'Prayansh',430);
    insert into studentMarks(id,name,total_marks) values( 2,'Rishika',345);
    insert into studentMarks(id,name,total_marks) values( 3,'Aayansh',390);         

Now lets open a transaction in a new session and try to read this table twice, and in between two read keep a delay of 18 Sec

Open Session1, and execute below transaction
    begin tran
    select * from studentMarks where ID in(1,2)
    waitfor delay '00:00:18'
    select * from studentMarks where ID in (1,2)
    rollback

Parallely open another session and execute below session
update studentMarks
set    total_marks=499 where id=1

Now the output from the first session you will find
 id          name         total_marks
----------- ----------------------
1           Prayansh      430
2           Rishika       345

(2 row(s) affected)

id          name          total_marks
----------- -------------------------
1           Prayansh      499
2           Rishika       345

(2 row(s) affected)

Here if you look at session1 output, a same select statement gives two different outputs, that is because , while session one is waiting for 18 second, session two updated the marks of id=1 to 499.

This is a problem with repeatable read if any outside transaction updates the data in between two reads.

How to avoid it
It can be avoided by setting transaction isolation level to repeatable read, let’s see how

set transaction isolation level repeatable read
    begin tran
    select * from studentMarks where ID in(1,2)
    waitfor delay '00:00:15'
    select * from studentMarks where ID in (1,2)
    rollback

Now here what will happen, if there is any outside transaction tries to modify the data which affect the Transaction one query then, the outside transaction will go on wait for getting update lock until transaction1 complete its operation.



Post Reference: Vikram Aristocratic Elfin Share

Wednesday, September 10, 2014

Stimulating DEADLOCK in SQL Server

Here in this article we are trying to stimulate Deadlock situation, we are trying to create two table table1 and table2 for the same purpose

create table table1
(id int identity(1,1),
col2 int)

create table table2
(id int identity(1,1),
col2 int)

Command(s) completed successfully.

Now lets insert few records in both the table.

insert into table1
select 10 union all
select 20 union all
select 30

insert into table2
select 100 union all
select 200 union all
select 300

No w open a session by opening a new query window in SSMS, I will call it session1 and try to execute the below code, here we opened a transaction then updating table1 col2 where id =1

begin tran
       update table1
       set col2 = col2 + 10
       where id = 2

Now open another query window (second session) in SSMS and execute the below statement, here we are trying to update both the table the second table will wait for first transaction to unlock table1 for its update. And it goes on execution for infinite time.

begin tran
       update table2
       set col2 = col2 + 100
       where id = 2

       update table1
       set col2=col2 + 10
       where id = 2
commit tran

Now again come back to session1 window and execute the below code

update table2
set col2 = col2 + 100
where id = 2

now as you run this above statement on session1, SQL Server sense deadlock since the above code wait for table2 to get unlocked by session2 and same way session2 waits for session1 for table1 to unlock and DEADLOCK occurs, in this case SQL server chosen session1 as victim and abort session1 with a message

Msg 1205, Level 13, State 45, Line 7
Transaction (Process ID 55) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

And session 2 will complete automatically.

There are no nights for SQL Developers, if so please give me address  J


Post Reference: Vikram Aristocratic Elfin Share

Monday, October 14, 2013

Pause your execution of block till a particular time using WAITFOR TIME


If you want your part of code to execute after a particular time then you can achieve this with the help of SQL Server WAITFOR TIME clause here you have mention the time along with the WAITFOR.

Let’s take a short example to see how we can achieve this. THRIST

select GETDATE() as 'Time'
WAITFOR TIME '05:40:00'
select GETDATE() as 'Delayed Time'
SELECT 'Hi its Morning'


Time
-----------------------
2013-10-10 05:39:56.440

(1 row(s) affected)

Delayed Time
-----------------------
2013-10-10 05:40:00.000

(1 row(s) affected)

Hi its Morning

Here we can see how we are able to achieve the delay of our batch using WaitFor Time.

Never miss a good chance to do good piece code.


Post Reference: Vikram Aristocratic Elfin Share

Pause your execution of block or delay execution using WAITFOR DELAY

There are situation when you want your piece of code to wait for some input after it proceed for next group of statement, if the scenario is like this you can achieve your wait using WAITFOR. So SQL Server has an option to wait for delay your batch until some short of input arrived.

Let’s take a short example to see how we can achieve this.

select GETDATE() as 'First Time'
waitfor delay '00:00:03'
select GETDATE() as '3 Second delay'
waitfor delay '00:00:02'
select GETDATE() as '2 Second delay'
waitfor delay '00:00:01'
select GETDATE() as '1 Second delay'

First Time
-----------------------
2013-10-10 05:24:39.007

(1 row(s) affected)

3 Second delay
-----------------------
2013-10-10 05:24:42.007

(1 row(s) affected)

2 Second delay
-----------------------
2013-10-10 05:24:44.007

(1 row(s) affected)

1 Second delay
-----------------------
2013-10-10 05:24:45.007

(1 row(s) affected)

Here we can see how we are able to achieve the delay of our batch using WaitFor Delay.

The way SQL developer says Database is poles apart. You database is safe as tongue in your mouth.


Post Reference: Vikram Aristocratic Elfin Share