
Locking Mechanisms in SAP
When multiple users access the same database table through different programs, these accesses must be synchronized to ensure consistency. Locks are used for this synchronization.
Locks are a method that helps coordinate each user's access to resources. Before accessing data, each user will initiate a lock so that no other user can do so at the same time.
Creating a Lock for a Database Table
- Access transaction SE11
- Select "Lock Object" and give it a name
- Click on "CREATE"
- Provide a description
- In the "TABLE" tab, specify the table to lock and the lock mode.
- The "Lock Parameter" tab displays the parameters by which the table will be locked.
- Save and activate.
This generates function modules that can be viewed in the "GOTO" => "LOCK MODULES" menu option.
- ENQUEUE_ZTABLE_XXXXXX - Request Lock
- DEQUEUE_ZTABLE_XXXXXX - Release Lock
Utilization within ABAP programs
Accessing the ABAP Editor (via transaction SE38 or SE80)
- LOCKING
- Lock Function: Menu => Model by opening a new window
- CALL FUNCTION: Enter the Name (ENQUEUE) assigned by transaction SE11 when created. The system automatically generates the CALL with all the parameters, including the Field Names that need to be completed to lock the record while working on it.
CALL FUNCTION 'Z_TRANSACTION_LOCKING'
EXPORTING
TRANSACTION = 'SPRO'
LOCK = 'X'
UNLOCK = space
EXCEPTIONS
ACTION_NOT_DEFINED = 1
ONLY_ONE_ACTION_ALLOWED = 2
UNKNOWN_TRANSACTION = 3
OTHERS = 4
.
IF SY-SUBRC = 0.
write: / 'Sucessfully locked Customizing'.
else.
write: / 'Error locking customizing'.
ENDIF.
- UNLOCKING
- Lock Function: Menu => Model by opening a new window
- CALL FUNCTION: Enter the Name (DEQUEUE) assigned by transaction SE11 when created. The system automatically generates the CALL with all the parameters, including the Field Names that need to be completed to release the record from the Lock.
Transaction SM12
In transaction SM12, a list of locked tables in the system can be viewed. This transaction helps to unlock any object in the system that may have been locked by a user who is offline, possibly due to a connection failure with SAP.