Enqueue and Dequeue: Lock Program in ABAP

by | Jun 4, 2018 | ABAP Programs

Home » SAP » ABAP » ABAP Programs » Enqueue and Dequeue: Lock Program in ABAP

Preface – This post is part of the ABAP Programs series.

Lock Objects in SAP ABAP are global reusable component which generates function modules i.e. ENQUEUE_E_TABLE and DEQUEUE_E_TABLE that are used to set and release locks on data record. This Enqueue and Dequeue method is used to lock and unlock any object in SAP ABAP. In this article, we will learn these methods with the help of a program. Before starting this, you must read the basic concepts of Lock Objects in SAP ABAP.

Introduction

It is very important to lock an object that is under development. By object we mean everything i.e. Tables, Views, Reports, Methods, Function Modules, and all. ABAP too provides these features by default for their standard interfaces. But what if you need to create one for your own table. Then you need to use the given two Function Modules:

  1. ENQUEUE_E_TABLE:  To Add Lock
  2. DEQUEUE_E_TABLE: To Remove Lock
Enqueue and Dequeue in ABAP- Image Illustration

Enqueue and Dequeue in ABAP- Image Illustration

ABAP Program to perform Enqueue and Dequeue Operations

DATA: varkey LIKE rstable-varkey.
varkey = sy-mandt.
CALL FUNCTION 'ENQUEUE_E_TABLE' "Add Lock
  EXPORTING
*   MODE_RSTABLE   = 'E'
    tabname        = 'ZBarry_TEST'
    varkey         = varkey
*   X_TABNAME      = ' '
*   X_VARKEY       = ' '
*   _SCOPE         = '2'
*   _WAIT          = ' '
*   _COLLECT       = ' '
  EXCEPTIONS
    foreign_lock   = 1
    system_failure = 2
    OTHERS         = 3.
CASE sy-subrc.
  WHEN 1.
    MESSAGE i184(bctrain) WITH 'Foreign lock'.
  WHEN 2.
    MESSAGE i184(bctrain) WITH 'system failure'.
  WHEN 0.
    MESSAGE i184(bctrain) WITH 'success'.
  WHEN OTHERS.
    MESSAGE i184(bctrain) WITH 'others'.
ENDCASE.
CALL FUNCTION 'DEQUEUE_E_TABLE' "Remove Lock
  EXPORTING
*   MODE_RSTABLE       = 'E'
    tabname = 'ZBarry_TEST'
    varkey  = varkey
*   X_TABNAME          = ' '
*   X_VARKEY           = ' '
*   _SCOPE  = '3'
*   _SYNCHRON          = ' '
*   _COLLECT           = ' '
  .
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

Code Explanation

The above program is self explanatory, still we will explain key points below:

  • Initially, we have defined  a variable varkey which will give the client information. It tells, ABAP, for which client, you want to block/unblock the table. It is taken automatically using sy-mandt.
  • Then we have called Function ‘ENQUEUE_E_TABLE’, it returns either either success, failure or Foreign lock exception.
  • Later, when we are done with our changes regarding our table, we need to unlock the same. For that we call Function ‘DEQUEUE_E_TABLE’.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author