This blog is inspired by an interview today. I would just like to refresh my knowledge gained in my university 🙂


We can get the concept of deadlock in wikipedia.

The picture below gives a common scenario which leads to deadlock.

/wp-content/uploads/2016/06/1_983599.png

So how can we write an ABAP program which will lead to deadlock? If we search for ABAP help, we can know it is possible that SELECT SINGLE FOR UPDATE can generate a deadlock.


/wp-content/uploads/2016/06/2_983600.png

So my test program is written based on SELECT FOR UPDATE.

The underlying database table has the following two entries:

/wp-content/uploads/2016/06/clipboard1_983602.png

Test1 – program instance 2 waits program instance 1 to release resource

I have written the following simple program:

/wp-content/uploads/2016/06/clipboard2_983603.png


Execute it as program instance 1:

/wp-content/uploads/2016/06/clipboard3_983604.png

then type /nSE38, execute this program once again as program instance 2. This time the program instance 2 hangs in line 10, waiting for the program instance 1 to release the lock.

/wp-content/uploads/2016/06/clipboard4_983605.png

Switch back to program instance1, click F8 to continue. Then the program instance 2 now is able to continue:

/wp-content/uploads/2016/06/clipboard5_983606.png

Test2 – deadlock

Write two different programs:


REPORT zlock1.
DATA: ls_prod TYPE zorder_header.
SELECT SINGLE FOR UPDATE * FROM zorder_header INTO ls_prod WHERE object_id = 'Z01'.
SELECT SINGLE FOR UPDATE * FROM zorder_header INTO ls_prod WHERE object_id = 'Z02'.
REPORT zlock2.
DATA: ls_prod TYPE zorder_header.
SELECT SINGLE FOR UPDATE * FROM zorder_header INTO ls_prod WHERE object_id = 'Z02'.
SELECT SINGLE FOR UPDATE * FROM zorder_header INTO ls_prod WHERE object_id = 'Z01'.

step1 – execute zlock1 under debug mode

/wp-content/uploads/2016/06/clipboard6_983607.png

step2 – execute zlock2 under debug mode

/wp-content/uploads/2016/06/clipboard2_983603.png

step3 – ZLOCK1 tries to lock Z02

/wp-content/uploads/2016/06/clipboard3_983604.png

step4 – go back to ZLOCK2, execute line 12

/wp-content/uploads/2016/06/clipboard4_983605.png

Result: runtime error in ZLOCK2, and ZLOCK1 now successfully locked both Z01 and Z02:

/wp-content/uploads/2016/06/clipboard5_983606.png

ST22 has given detailed information:

/wp-content/uploads/2016/06/clipboard6_983607.png

Further reading

You can find how to write a Java program to generate deadlock and how to detect deadlock in Java program via standard JDK tool jstack from this blog.

To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

  1. Suhas Saha

    Well described! 😎

    Did you check if SELECT SINGLE FOR UPDATE produces an “exclusive” SAP-Lock? May be screenshots from SM12 would have been better.

    BR,

    Suhas

    (0) 
    1. Jerry Wang Post author

      Hello Suhas,

      When I made the test mentioned in this blog, I didn’t find entry in SM12. It seems SM12 can only monitor SAP lock acquired by enqueue function module. The SELECT SINGLE FOR UPDATE only generates database lock, but not SAP lock. You can also have a try yourself.

      Best regards,

      Jerry

      (0) 
      1. Suhas Saha

        Hi Jerry,

        i had guessed so. SAP documentation states,

        When reading an individual row using SINGLE, the addition FOR UPDATE sets an exclusive lock for this row on the database.

        This means DB-lock and not SAP-Lock, which explains why you did not find the lock record in SM12 🙂

        Thanks,

        Suhas       

        (0) 
        1. Thomas Zloch

          Database locks can be monitored via DB01, the display is very DB-dependent.

          Not sure why I’d ever want to provoke any, can happily live without them 😉

          Cheers

          ZLOCK2

          (0) 

Leave a Reply