Translate

Wednesday, 23 May 2012

Logical Unit of Work


Logical Units of Work in ABAP, (LUWs in SAP)

LUWs are one of the least understood concept in ABAP, even by experienced ABAPers. More over it is one of the most important concept in ABAP.

Knowledge of SAP Transactions, SAP LUW and Database LUW is crucial in ensuring that your database updates does not result in inconsistent data.

Let us take an example: 
Consider a Transaction (ABAP program), where intention is to Debit Rs 50 from account A, and credit those Rs 50 into account B.
Say debit from account A is successful. But when there was a credit of Rs 50 to account B, an error happens hence crediting did not happen. Bust as you have noticed, debiting from account A is already odne, and now this Rs 50 is lost. ( Neither in Account A, nor in Account B )

Transaction: An application transaction, possibly containing multiple screens, where multiple tables need to be updated.


Database LUW: System's approach of updating database. Either an update happens completely or does not happen at all. System has fixed rules, based on which a DB LUW ends/starts.

SAP LUW: A series of logically related updates. Ideally an SAP LUW should contain only one DB LUW, to enure that data in consistent. If a SAP LUW is spread across several DB LUWs, it can result in inconsistent data.

Rules related to Database LUW
Current DB LUW ends, and new one starts: 
1. While moving form one screen to another screen (as soon a dialog step completes).
2. While calling a RFC
3. While exiting from and RFC
4. When encouters a WAIT statement
5. When any message is sent to dialogue. (Information, warning, error)
6. When COMMIT WORK statement is encountered
7. When function module DB_COMMIT is called.


Example of defective programming.
<-------------------------------SAP Transaction----------------------------------->
<---------------SAP LUW-------------><-------------SAP LUW----------------->
<--DB LUW--><--DB LUW--><--DB LUW--><--DB LUW--><--- DB LUW-->


Example of right programming technique.
<-------------------------------SAP Transaction----------------------------------->
<---------------SAP LUW-------------><-------------SAP LUW----------------->
<--------------DB LUW---------------><-----------------DB LUW--------------->

Why cant I use ROLLBACK statement? 
ROLLBACK statement scope in only within a DBLUW. I.e. only the changes done within the current DB LUW will be undone. Changes done in previous DB LUWs will not be undone.

How to ensure that an SAP LUW contains only one DB LUW?
There are two approaches
1. Using CALL FUNCTION  ............... IN UPDATE TASK statement.
2. Using PERFORM ....... ON COMMIT statement.

Other approach
This approach can be specially used/needed in HR Function modules.
Example: Creating a Org Unit and relationships. Requirement is that either Org Unit and all its relationships should get created, else nothing should get created.

In this approach we use FMs that provide option to handle buffers. These FMs have an import parameter VTASK, If VTASK is passed as 'B', update will happen only in buffer, not in DB. Once all update FMs are called with VTASK = B, call the FM RH_UPDATE_DATABASE to update the database.

No comments: