Monday, 24 July 2017

LOCK OBJECT

LOCK OBJECT

These types of objects are used for locking the access to database records in table. This mechanism is used to enforce data integrity that is two users cannot update the same data at the same time. With lock objects you can lock table-field or whole table.

In a system where many users can access the same data, it becomes necessary to control the access to the data. In R/3 system this access control is built-in on database tables. Developers can also lock objects over table records.

To lock an object you need to call standard functions, which are automatically generated while defining the lock object in ABAP/4 dictionary. This locking system is independent of the locking mechanism used by the R/3 system. Whenever an object is locked, either by in built locking mechanism or by function modules, it creates corresponding entry in global system table i.e. table is locked. The system automatically releases the lock at the end of transaction.

Creating Lock Objects

Lock object is an aggregated dictionary object and can be defined by using the following steps:

1. From initial data dictionary screen, enter the name for the object, Click Lock object radiobutton and then click on Create. The system displays a dialog box for Maintain Lock Objects screen
2. Enter short text as usual and the name for primary table.
3. Save
4. Select Tables option
From this screen you can: Select secondary tables, if any, linked by foreign key relationship.

Fields for the lock objects. This option allows you to select fields for objects (R/3 system allows locking up to record level). Lock object argument are not selected by user but are imposed by the system and includes all the primary keys for the table.


Creating Lock Objects in SAP


Lock Objects are the part of data dictionary objects, lock objects can be created in SE11 T-code, lock objects always starts with E, (these are the only custom objects that dosen`t start with Z in SAP).

Creating a lock object in SAP

Lock objects are data dictionary objects, can be created in SE11, follow below steps to create lock object.
Example: Here I am creating a lock object to insert, delete, update for table ZSTUDENT table in SAP.
Go to SE11, provide lock object name as EZSTUDENT, create

Provide short description, select tables tab, provide table name and lock mode, save and activate
When ever we create a lock object two function modules will be created, go to SE37, check FM`s DEQUEUE_EZSTUDENT and ENQUEUE_EZSTUDENT.

Using the lock object to update/insert table

When ever we create a lock object, two function modules(ENQUEUE and DEQUEUE) will be created, we have to call those function modules before and after open SQL statements, refer program below.

REPORT ZSAN_LOCKOBJECT.

DATA : IT_STUDENT TYPE TABLE OF ZSTUDENT,
       WA_STUDENT TYPE ZSTUDENT.

WA_STUDENT-STUDENTID = '09'.
WA_STUDENT-NAME = 'SAPNuts'.
WA_STUDENT-LASTNAME = 'SAPNuts'.
WA_STUDENT-FATHER = 'SAP'.
WA_STUDENT-DOB = '25/09/2013'.
WA_STUDENT-GENDER = 'Male'.
WA_STUDENT-COURSE = 'SAP ABAP'.
WA_STUDENT-EMAIL = 'admin@sapnuts.com'.
WA_STUDENT-FEE = '0000'.

CALL FUNCTION 'ENQUEUE_EZSTUDENT' "add lock
  EXPORTING
    MODE_ZSTUDENT = 'E'
   MANDT         = SY-MANDT
   STUDENTID     = WA_STUDENT-STUDENTID  "record to lock, optional parameter
*   X_STUDENTID   = ' '
*   _SCOPE        = '2'
*   _WAIT         = ' '
*   _COLLECT      = ' '
* EXCEPTIONS
*   FOREIGN_LOCK  = 1
*   SYSTEM_FAILURE       = 2
*   OTHERS        = 3
  .
IF SY-SUBRC = 0.
  MODIFY ZSTUDENT FROM WA_STUDENT.
ENDIF.

CALL FUNCTION 'DEQUEUE_EZSTUDENT' "release lock
  EXPORTING
    MODE_ZSTUDENT = 'E'
   MANDT         = SY-MANDT
   STUDENTID     = WA_STUDENT-STUDENTID "record to release lock, optional parameter
*   X_STUDENTID   = ' '
*   _SCOPE        = '3'
*   _SYNCHRON     = ' '
*   _COLLECT      = ' '



Types of locks

You can lock the table or record by using following types of locking:

Exclusive (E) or Read mode: The locked data can only be displayed or modified by single user i.e. the owner of the object. Access to other users is denied.

Shared (S) or Write Mode: Several users can access the same record simultaneously, but only in display mode and except the first one, who has asked for the data in update mode.

Exclusive not cumulating (X) it is similar to exclusive lock. It allows only a single user access. E can be called several times from the same transaction. In contrast, a lock type X can be called only once during the transaction. Any other call for this lock is rejected.

When you activate the lock object, the functions are automatically generated. And these are ENQUEUE-EZN and DEQUEUE-EZN. EZN is name of the lock object.

While ENQUEUE is used in program to set the code over the selected data depending upon the lock object arguments. DEQUEUE is used to release the lock.