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

Using COMMIT WORK in User Exit or in BADI

Former Member
0 Likes
11,287

Hi all,

Is it allowed if I want to use COMMIT WORK in code written in the User Exit or BADI?

Please help me in this topic.

Lokking for a lot of ur quick replies.

Thanks in advance.

Best Regards,

Prasad

8 REPLIES 8
Read only

Former Member
0 Likes
4,679

Hi Prasad,

You should not use the COMMIT WORK inside the BADI or USER EXIT as it will lead to inconsistancies in data.

Note : Commit and Roll back shold be avoided ( You Might have to use the Commit Work in some cases as of ur need )in User exit and BADIs Because while executing its not always sure that all the values needed for database updations are present or not.

Example.

Suppose steps 1,2,3,4 have to be completed before the transaction completes its Logical Unit of Work and calls the internal Commit Work.

Now you are adding some piece of code through a User exit between step 2 and 3.

If you use a commit work, All the data manipulated in Step 1 and 2 will be saved.

however if somehow step 3 gets failed.(Which means LUW brakes and there should not be any database commit)

Evrythng should ROLLBACK at that time,including the data in step 1 and 2.

But it will not happen, as you have already used a Commit Work inside ur exit.

Hope this clarifies ur doubt

regards,

Debasish.

Read only

former_member192854
Active Participant
0 Likes
4,679

Hi,

if you are not allowed to use COMMIT WORK in your user-exit, I recommend you to use the function module 'BAPI_TRANSACTION_COMMIT'.

Regards,

Sander

Read only

0 Likes
4,679

Hi,

Use of 'Commit work' or FM 'BAPI_TRANSACTION_COMMIT' is not at all allowed in standard SAP transactions User-exit or BADI as this can lead to multiple commit works in standard program (one added in user-exit and other is in standard program).

Having multiple commit work statements in standard programs, many times leads to inconsistencies in database update because program will try to insert/update data in database multiple times which will fail.

So its good to avoid the use of 'commit work'.

Sumit

Read only

0 Likes
4,679

>if you are not allowed to use COMMIT WORK in your user-exit, I recommend

>you to use the function module 'BAPI_TRANSACTION_COMMIT'.

This is an incorrect thing to do! The function BAPI_TRANSACTION_COMMIT is just a wrapper around the COMMIT WORK statement so that it can be called as part of a remote transaction via RFC. This will have the exact same bad effects as already documented by others in this thread as calling COMMIT WORK.

Within your user-exit you have the option of:

- Doing DB update, but don't commit them and when the overall transaction is commited by the standard transaction then your updates will also be commited.

- Putting your updates into an Update Task function module and calling this during the user-exit. It won't actually be executed until the main transaction calls the COMMIT WORK and then at that time it will be executed in an update process along with the other standard update fucntions as part of the transaction.

Key thing, don't call COMMIT WORK or call BAPI_TRANSACTION_COMMIT.

Now that I've said that, here is a scenario that I've had in the past. During user-exit I need to do updates to custom tables or call APIs that do updates to the DB and I need them to be commited immediatly as a seaperate DB transaction from the main calling program (Note, this is a very specific requirment becuase there is no roll-back possible). The way to accomplish this is to put the updates and your COMMIT WORK into a function module and mark the function module as RFC enabled (note, does not need to be Update enabled because this isn't an update task). In the User-Exit call the new function with the addition "Starting New Task"

>CALL FUNCTION func STARTING NEW TASK task

> [DESTINATION {dest|{IN GROUP {group|DEFAULT}}}]

> parameter_list

> [{PERFORMING subr}|{CALLING meth} ON END OF TASK].

By doing this function call in a new task it is actaully making an RFC to another process on one of the application servers and will be handled as a seperate DB transaction from the main program.

So final note on this one, don't call COMMIT WORK or call BAPI_TRANSACTION_COMMIT but if you have a specific need to create a seperate DB transaction you can use this method that I've described but remember that there is no way to roll back the changes of the seperate transaction.

~Ian

Read only

0 Likes
4,679

Hi Maxwell ,

Thanks for you answer , it cleared me concept of commit in badi/user exit/enhancement .

can you please confirm my understanding ?

my understanding :

Situation :

i have in badi :

Select * from ZCUSTOM into table ITAB.

Read Table ITAB into WA_ITAB with key

WA_ITAB - Delivered_Quantity = WA_ITAB - Delivered_Quantity + 5 .

Modify table ITAB from WA_ITAB.  

If NOT ITAB[] IS INITIAL.

  MODIFY ZCUSTOM from ITAB.  ""updatin the DB

Commit work .

endif.

so as u told we should not use commit work inside , hence we need to use this piece of code inside a FM and call that FM as CALL 'XXX' in update task .

so after badi completes its transaction , a standard COMMIT WORK is triggered and after that COMMIT WORK is triggered our FM 'XXX' will be executed ?

is my understanding is write ?

Awaiting for your response .

thanks,

Praveeen.

Read only

Former Member
0 Likes
4,679

Dear All,

Thanks a lot for the replies.

Actually my requirement is also not to update immediately.

But, problem I am facing is, what I want to update gets overwritten at the last COMMIT in the standard Program.

i want to overcome that.

So, could u please suggest the solution for that.

Looking for ur a lot of replies.

Thanks a lot again.

Best Regards,

Prasad

Read only

Former Member
0 Likes
4,679

Question solved

Read only

0 Likes
4,679

Hi Prasad,

May I know how you solve your problem? I have the same problem as well.

Regards,

Dale