Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Programming interview questions: write an application that will cause database deadlock

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

I believe that everyone can successfully complete the requirements such as "developing a Java application that will produce deadlocks". But if the problem is to be more specific and the deadlock is required to occur at the database level, how should it be done?

Here I provide an answer, implemented in ABAP (Advanced Business Application Programming), SAP's programming language.

We know from the ABAP help documentation that when SELECT SINGLE FOR UPDATE reads a record from the database, it locks the record in the database. It is also mentioned in the help documentation that if programming is not appropriate, it can cause deadlock.

So we use the sentence SELECT SINGLE FOR UPDATE to answer the question.

First, insert two records in the database, the primary keys are Z01 and Z02.

Two applications are developed. The first application locks Z01 and Z02 in turn.

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. The second application locks Z02 and Z01 in turn. 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'.

The following steps will cause a deadlock at the database level.

1. Run the first application in debug mode, step through line 10 of the code, and successfully lock Z01.

two。 Open a new window to run the second application in debug mode, step through line 10 of the code, and successfully lock Z02.

3. Go back to the window of Application 1 and continue. At this point, Application 1 attempts to lock Z02, but Z02 is already locked by Application 2, so Application 1 is waiting.

4. Go back to the window of Application 2 and continue. At this point, Application 2 attempts to lock Z01, but Z01 is already locked by Application 1, so Application 2 can only wait for Application 1 to release the lock of Z01. But application 1 is also waiting for application 2 at this time, resulting in a deadlock.

ABAP is different from Java. Once a deadlock is detected, the application throws a runtime exception and terminates automatically. The exception message is very clear: Deadlock detected while executing transaction....

For more original Jerry technical articles, please follow the official account "Wang Zixi" or scan the following QR code:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report