Lock Objects

The SAP System synchronizes simultaneous access of several users to the same data records with a lock mechanism. When interactive transactions are programmed, locks are set and released by calling function modules . These function modules are automatically generated from the definition of lock objects in the ABAP Dictionary.

Structure of a Lock Object

The tables in which data records should be locked with a lock request are defined in a lock object together with their key fields. When tables are selected, one table (the primary table) is first selected. Further tables (secondary tables) can also be added using foreign key relationships 
This graphic is explained in the accompanying text

Lock Arguments

The lock argument of a table in the lock object consists of the key fields of the table.
The lock argument fields of a lock object are used as input parameters in the function modules for setting and removing locks generated from the lock object definition. When these function modules are called, the table rows to be locked or unlocked are specified by defining certain values in these fields. These values can also be generic. The lock argument fields therefore define which subset of the table rows should be locked.
This graphic is explained in the accompanying text
The simplest case of a lock object consists of exactly one table and the lock argument of the table is the primary key of this table. Several tables can also be included in a lock object. A lock request therefore can lock an entire logical object, and not only a record of a table. Such a logical object can be for example a document comprising an entry in a header table and N entries in a position table.
This graphic is explained in the accompanying text
Locks can also be set from programs in other systems with the corresponding interfaces if the lock object was defined with RFC authorization.
A lock mode can be assigned for each table in the lock object. This mode defines how other users can access a locked record of the table.
Example
Table SFLIGHT in the flight model contains all the scheduled flights of a carrier. Field SEATSMAX contains the number of seats available. Field SEATSOCC contains the number of seats already booked. If a booking is made for a customer (by a travel agency or sales desk), you must check whether there are enough seats available. The number of seats booked is incremented when the booking is made.
This mechanism must ensure that two sales desks do not make the same booking at the same time and that the flight is not overbooked.
This can be done by creating lock object ESFLIGHT. Only the table SFLIGHT must be included in this lock object. The flight can then be locked (with the function modules generated from the lock object) when booking. If another sales desk also wants to book seats for this flight, the lock will prevent the flight from being overbooked.


 Example for Lock Objects 
When booking flights (see Flight Model) it is important to prevent flights from being overbooked. For this reason, you have to lock the particular flight as well as all the bookings existing for this flight during processing. You can do this with lock object E_BOOKING.
The flights are recorded in table SFLIGHT and the bookings for the flights in table SBOOK. The two tables are linked with a foreign key. Lock object E_BOOKING must therefore contain table SFLIGHT as primary table and table SBOOK as further table.
This graphic is explained in the accompanying text
The lock argument of table SFLIGHT thus contains the fields MANDT, CARRID, CONNID, and FLDATE. The lock argument of table SBOOK thus contains the fields MANDT, CARRID, CONNID, FLDATE, BOOKID and CUSTOMID.
Select exclusive lock mode, that is the locked data can only be displayed and edited by one user.
When the lock object is activated, the following function modules are generated from its definition:
  • ENQUEUE_ E_BOOKING (set locks)
  • ENQUEUE_ E_BOOKING (release locks)
These function modules can now be linked to ABAP programs.
The following example shows how function module ENQUEUE_ E_BOOKING is called.
This graphic is explained in the accompanying text
With this call, flight LH 400 on Nov. 29,1998 is exclusively (lock mode E) locked in table SFLIGHT together with all the bookings entered in table SBOOK for this flight (since the initial value 0 is transferred for BOOKID and CUSTOMID). The lock is sent to the update program (_SCOPE = ‘2’). If there is a lock conflict, another attempt is made to set the lock after a certain time (_WAIT = ‘X’).
The set locks can be removed by calling the function module DEQUEUE_E_BOOKING as follows:
This graphic is explained in the accompanying text



Creating Lock Objects  Locate the document in its SAP Library structure

Procedure

       1.      Select object type Lock object in the initial screen of the ABAP Dictionary, enter an object name and choose This graphic is explained in the accompanying textCreate. The name of a lock object should begin with an E (Enqueue).
The maintenance screen for lock objects is displayed.
       2.      Enter an explanatory short text in the field Short text.
You can then use the short text to find the lock object at a later time, for example with the SAP Repository Information System.
       3.      Enter the name of the primary table of the lock object.
All other tables in the lock object must be linked with the primary table using foreign keys. There are also some restrictions on the valid foreign key relationships.
       4.      Select the lock mode of the primary table in the field below it.
The lock mode is used as the default value for the corresponding parameters of the function modules generated from the lock object.
       5.      Choose Add if you want to lock records in more than one table with the lock object.
A list of all the tables linked with the primary table using valid foreign keys is displayed. Select the appropriate table. The lock mode of the primary table is copied as lock mode. You can change this setting as required, for example you can assign the lock mode separately for each table.
Similarly, you can add a table linked with the secondary table just added with foreign keys. To do this, place the cursor on the name of the secondary table and choose Add.
Note
If no lock mode is assigned to a table, no lock is set for the entries in this table when the generated function modules are called. You should not assign a lock mode if a secondary table was only used to define a path between the primary table and another secondary table with foreign keys.
       6.      Save your entries.
A dialog box appears in which you have to assign the lock object a development class.
       7.      You can (optionally) exclude lock parameters (see lock objects) from the function module generation on the Lock parameter tab page. This makes sense for example if you always want to lock a parameter generically.
To do this, simply deselect the Weight flag for the parameter. The parameter is not taken into consideration in the generated function modules. This parameter is then always locked generically.
The name of a lock parameter is usually the name of the corresponding table field. If two fields with the same name are used as lock parameters in the lock object from different tables, you must choose a new name for one of the fields in field Lock parameter.
       8.      You can define whether the function modules generated from the lock object should be RFC-enabled on the Attributes tab page.
If you set the Allow RFC flag, the generated function modules can be called from within another system with Remote Function Call.
If you permit Remote Function Calls for an existing lock object, you must ensure that the generated function modules are called from within an ABAP program with parameters appropriate for the type.  You should therefore check all programs that use the associated function modules before activating the lock object with the new option.
       9.      Choose This graphic is explained in the accompanying text.

Result

When you activate the lock object, the two function modules ENQUEUE_<lockobjectname> and DEQUEUE_<lockobjectname> are generated from its definition to set and release locks.
You can find information about the activation flow in the activation log, which you can display with Utilities Activation log. If errors occurred during activation, the activation log is displayed immediately.




Deleting Lock Objects  Locate the document in its SAP Library structure
Prerequisites
When you delete a lock object, the function modules generated when you activated the lock object are automatically deleted as well. These generated function modules might still be in use in programs or classes.
Therefore, before deleting a lock object, find all programs or classes that contain these function modules and remove the calls to the function modules.
Procedure
  1. In the initial screen of the ABAP Dictionary, select object type Lock object and enter the lock object name.
  2. Choose This graphic is explained in the accompanying text to find all the programs or classes that are still using the lock object. Remove the lock module calls in the objects you found.
  3. Choose This graphic is explained in the accompanying text.
  4. A dialog box appears in which you must confirm the deletion request. If the function modules belonging to the lock object are still in use in programs or classes, a corresponding warning appears. In this case you must adjust the programs or classes affected before deleting the lock object.
  5. Confirm the deletion request.
Result The lock object is deleted together with the function modules generated from this lock object.

No comments:

Post a Comment