Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to prevent two DB update inconsistent issue?

0 Likes
2,018

Hello Community,

is there a possibility to prevent two DB update inconsistent?

I'm working on a program that want to update two add-on tables at the same time, but sometimes only header table (zheader) commit successfully, item table (zitem) not been update but without any exception.

Is any issue for below code? or I need to use in "update task update" for below update?

MODIFY zheader FROM TABLE it_zheader.
IF sy-subrc = 0. 
  l_flag = 'X'.
ELSE.
  l_flag = ''.
ENDIF.


MODIFY zitem FROM TABLE it_zitem.
IF sy-subrc = 0 AND l_flag = 'X'.
  l_flag = 'X'.
ELSE.
  l_flag = ''.
ENDIF.

IF l_flag = 'X'.
   COMMIT WORK AND WAIT.
ELSE.
  ROLLBACK WORK.
  MESSAGE s398 DISPLAY LIKE 'I' WITH 'Update table Successfully!'.
ENDIF.

Thank you.

Regards,

Nicole

Hello Community,

is there a possibility to prevent two DB update inconsistent?

I'm working on a program that want to update two add-on tables at the same time, but sometimes only header table (zheader) commit successfully, item table (zitem) not been update but without any exception.

Is any issue for below code? or I need to use in "update task update" for below update?

MODIFY zheader FROM TABLE it_zheader.
IF sy-subrc = 0. 
  l_flag = 'X'.
ELSE.
  l_flag = ''.
ENDIF.


MODIFY zitem FROM TABLE it_zitem.
IF sy-subrc = 0 AND l_flag = 'X'.
  l_flag = 'X'.
ELSE.
  l_flag = ''.
ENDIF.

IF l_flag = 'X'.
   COMMIT WORK AND WAIT.
ELSE.
  ROLLBACK WORK.
  MESSAGE s398 DISPLAY LIKE 'I' WITH 'Update table Successfully!'.
ENDIF.

Thank you.

Regards,

Nicole

9 REPLIES 9
Read only

matt
Active Contributor
0 Likes
1,746

Please in future use the "code" button in the editor when posting code. I've done it for you this time.

Your coding is poor. First of all, l_flag is not a meaningful name for a variable. How about previous_modify_successful, and use the provided abap_true and abap_false instead of arbitrary '' and 'X'. But the structure is also not good. as you attempt an update of zitem even when the zheader update has already failed.

MODIFY zheader FROM TABLE it_zheader.
IF sy-subrc = 0.  
  MODIFY zitem FROM TABLE it_zitem.
  IF sy-subrc = 0 
    COMMIT WORK AND WAIT.
    db_modify_was_successful = abap_true.
  ENDIF.
ENDIF.
If db_modify_was_successful = abap_false.
  ROLLBACK WORK.
  MESSAGE s398 DISPLAY LIKE 'I' WITH 'Update table Successfully!'.
ENDIF

Do you know it_item isn't empty? Can you reproduce your observed issue? Have you tried debugging?

Read only

0 Likes
1,746

Dear Mattehew,
Thanks for your good comments. actually the structure in our production is not like this (this is just a sample), we have the logic in previously form to check it_item empty or not, and this program already run for some time in production environment. don't know why some times item table update failed (we had traced item structure not empty at that moment, no any exception or DB lock occurred).

Read only

RaymondGiuseppi
Active Contributor
1,746
  • Please move the success message in the successful IF case...
  • As MODIFY would, by conception, not raise an error for duplicate primary keys, you should look for secondary unique indexes and locked records (at database level , what did you code to prevent this case) or even in system or database log.
Read only

0 Likes
1,746

//Correct my code due my typo for message control as below, and highlight that I just need to update the exist record's status in these two tables, not insert new record, tks. 

MODIFY zheader FROM TABLE it_zheader.
IF sy-subrc = 0. 
  l_flag = 'X'.
ELSE.
  l_flag = ''.
ENDIF.

MODIFY zitem FROM TABLE it_zitem.
IF sy-subrc = 0 AND l_flag = 'X'.
  l_flag = 'X'.
ELSE.
  l_flag = ''.
ENDIF.

IF l_flag = 'X'.
   COMMIT WORK AND WAIT.
ELSE.
  ROLLBACK WORK.
  MESSAGE s398 DISPLAY LIKE 'I' WITH 'Update table Error!'.
ENDIF.
Read only

0 Likes
1,746

I just want to update some field's value for exist record's in these two tables, and I also can make sure that just no one update the same record at the same time, do I still need to lock the records?

Read only

0 Likes
1,746

You must use the SAP locking mechanism if there's a chance of concurrent update.

Read only

1,746

And each and every program that update those tables must use the same enqueue mechanism of course.

Read only

0 Likes
1,746

You mean use ENQUEUE and DEQUEUE lock mechanism to lock these two table? or any other?

Read only

0 Likes
1,746
  • Yes every program that update this table should use the same enqueue (e.g. E_TABLE)
  • The COMMIT/ROLLBACK WORK will remove the lock, so no explicit call of DEQUEUE should be required.