Article from http://weiba.weibo.com/10015/t/zkOgtyY0V
SAP Lock Mechanism
I. Why should I set a lock for sap:
1. Maintain Data Consistency
If several users want to access the same resource, they need to find a synchronous access method to maintain data consistency. For example, in the flight booking system, you need to check whether there are any empty seats. During the check, you do not want others to modify the important data (the number of empty seats ).
2. database lock alone is not enough.
The Database Management System physically locks the row record to be modified. Other users can access this record only after the database lock is released. In the SAP system, the database lock is released when a new screen is displayed, because the screen change triggers an implicit dB commit. If the data is collected from several screens, and the data will be locked separately during this period, it is not enough to use the database lock alone. The SAP system has a global lock table on the application server layer, which can be used to set logical locks to lock related table entries and enqueue workflow to manage these locks. The SAP lock is a logical lock. It is possible that the table entries you lock do not exist in the database at all.
Ii. Lock Object and its corresponding function module
Create a Lock Object in SE11. All custom lock objects must start with EZ or ey. A lock object contains only one primarytable, which can contain several secondary tables. There are three lock modes: E, S, and X. Set the fields in lock parameters to lock table entries.
Mode e: This mode is set when data is changed. Writelock (exclusive lock)
Mode S: No data needs to be changed, but the data to be displayed is not changed by others. Read lock (shared locked)
Mode X: similar to E, but not accumulative, exclusive. Enhancedwrite lock (exclusive lock without cumulating)
If you successfully lock a lock object in a program, if the mode is E, other users cannot add any lock in the E, X, or S mode to the lock object;
If you successfully lock a lock object in a program, if the mode is X, other users cannot add any lock in the E, X, or S mode to the lock object;
If you successfully lock a lock object in a program, if the mode is s, other users cannot add the E and X locks to the lock object, however, the S-mode lock can be applied;
If you successfully lock a lock object in a program, if the mode is E, you can also add the E and S modes to the lock object in this program, X mode is not supported.
If you successfully lock a lock object in a program, if the mode is X, in this program, you cannot add a lock in the E, X, or S mode to the lock object.
If you successfully lock a lock object in a program, if the mode is S, you can also add the S mode lock to the lock object in this program, if no other user adds the S-Mode Lock to it, you can also apply the e-Mode Lock to it. X mode is not supported.
When the Lock Object is activated, the system automatically creates two FM, enqueue _ <Lock Object Name> and dequeue _ <Lock Object Name>, which are used to lock and unlock the Lock Object respectively.
SAP provide three type of lock objects.
• Readlock (shared locked)
Protects read access to an object. The readlock allows other transactions read access but not write access
The locked area of the table
• Writelock (exclusive lock)
Protects write access to an object. thewrite lock allows other transactions neither read nor write access
The locked area of the table.
• Enhancedwrite lock (exclusive lock without cumulating)
Works like a write lock into t that theenhanced write lock also protects from further accesses from
Same transaction.
3. Lock and unlock
When you use a logical lock to lock table entries, the system automatically writes records to the lock table.
When you call the FM to set the lock, if the lock parameters parameter is not specified, the system locks the entire table. Of course, lock parameter: client is a bit special, if not specified, the default is SY-MANDT; if the corresponding client is specified, the corresponding table records on the corresponding client will be locked; if set to space, all clients are locked.
When logical lock settings fail, there are two exceptions. One is exception: foreign_lock, which means the lock has been made; the other is exception: system_failure. In some cases, logical locks set successfully in the program are implicitly unlocked. For example, when the program ends (when the message type is a or X), use the leave program, leave to transaction statement, or enter/N in the command line and press Enter.
At the end of the program, you can use the dequeue function module to unlock the program (if you do not write this, the program will be automatically unlocked at the end of the program). At this time, the system automatically deletes the corresponding records from the lock table. When the dequeue function module is used to unlock the instance, no exception is generated. To unlock all the logical locks you have created in the program, you can use FM: dequeue_all.
4. General steps for locking
Lock first. After the lock is successful, retrieve the data from the database, change the data, update the data to the database, and finally unlock the database. Follow this step to make sure that the changes are completely running under the lock protection mechanism.
The screen recording program for warehouse management involves converting tr to mb1b. It is estimated that the tr table is locked by the sap lock mechanism. If the delay
If the time is not enough, an error will be reported: TR is being processed.
After I use the function 'dequeue _ elltbke' to unlock the instance, Tr can be switched to the instance, but an error is still returned. I cannot think of it.
Cause. Help !~
Try adding a "Commit work" in the middle of theprocess.
Thank you for your advice. The little girl had an epiphany. The SAP lock mechanism is locked for both the database layer and the application service layer.
The application service layer is unlocked, and the database layer must use commit work.
Locking is mainly based on database requirements. Many tables are invisible at the database layer, so lock management should be completed on the SAP interface. A lock is a shared lock and a lock called a row.
Transaction code: sm12, which shares X below and is a shared lock. Yes, it means no change is allowed, but you can check it.
Exclusive locks are not allowed to be viewed or modified by others.
By default, 13000 locks are supported. Lock resources are in the locklist of a table in the memory. If they are all filled up, the lock fails and cannot be changed.
When someone else is locked, they cannot be locked.
The above are normal locks.
Another scenario is "deadlock". Die log, for example, user a and user B must use 1-4 processes, and user a must use 3, 4 for user 1, 2 for user B, so everyone occupies their own resources and waits for others to release the lock.
Check should lock all users.
Transaction code: db01. Check the deadlock.
To lock a lock, you must send a command to unlock it. For example, after the lock is completed, the machine will be down or the network will be disconnected, and the lock will occur and the machine will not be unlocked. Use the transaction code sm12 and delete it manually.
Deadlock is a serious problem for system performance.
Another situation is that some actions cannot be performed simultaneously.
Any changes to the database will be locked.
Time consumption: SD reverse rush, price, ATP check
Matrix P, MRP run
CO: item account
The lock is implemented through the enqueue server. If it is on the application server, it must be transmitted to the CI through the Message Server. If there are too many locks, the memory table has a predefined size, and you can try to reduce the number of locks (there is a technology called "lock upgrade "), try to increase the size of the lock table (increase the memory ). Generally, SAP locks a row and only locks one or some rows. In the database, the lock table is also limited. The lock upgrade uses a lock to lock the table. Table locking is controlled by a parameter. When a user locks more than 70%, the table is locked to reduce the total number of tables locked.
If a user sends a command, if the dialog suddenly breaks down or the user exits, no one will unlock it. The system administrator is required to participate. Transaction code: sm12. Check the lock time for more than three hours to find out the cause. There is another transaction code, db01, which is normally no lock.
You can also grant the user permission to delete your lock.
Simple understanding of SAP Lock Mechanism
Due to work needs, I recently studied the problem of Concurrent Data Control in the business system. I have written an ABAP program. Remember that sap has a lock object that can lock table records, so I learned about it and designed the MC-based lock mechanism on this basis (I have time to propose the MC-based lock mechanism for your criticism ). If you think that, as described below, you have already understood sap more than you have.
SAP uses a lock mechanism to synchronize multiple users to operate on the same data and prevent data inconsistency. Generally, SAP will set a lock before the operation data to prevent the second user from modifying the operation. After the operation is completed, the system will automatically release the lock (of course, the program can also call the unlock function to unlock the lock ).
1. SAP lock type
1) exclusive lock
The locked data can be read orprocessed by one user only. A request for another exclusive lock or for ashared lock is rejected.
2) shared lock
Several users can read the same dataat the same time, but as soon as a user edits the data, a second user can nolonger access this data. requests for further shared locks are accepted, evenif they are issued by different users, but exclusive locks
Are rejected.
3) exclusive but notcumulative lock
Exclusive locks can be requested bythe same transaction more than once and handled successively, but an exclusivebut not cumulative lock can only be requested once by a given transaction. allother lock requests are rejected.
2. tcode, related tables, and multi-Table locks
1) tcode: You can view and create a lock object through SE11. After the lock object is created, the system will automatically create two FM instances, enqueue _ <Lock Object Name> and dequeue _ <Lock Object Name> are used to lock and unlock respectively.
2) related tables:
Dd25l: Composite title (method, MC target, locked target) (record the lock master table );
Dd25t:
Dd26s: Relationship between the basic table and external code of the view (records all lock-related tables );
Dd27s: Total (view, MC object, locked object) field;
3) Multi-Table lock
Create a Lock Object in SE11. All custom lock objects must start with EZ or ey. A lock object contains only one primary table, which can contain several secondarytables. For example, multi-Table lock: emekkoe, master table: Ekko --> E dedicated accumulation, from table: Ekpo --> E dedicated accumulation.
3. Lock Lifecycle
Generally, when a program is run, it needs to enqueue the Special Records. After the program is run, it will perform dequeue processing (if the processing is not displayed, SAP will automatically dequeue when the session is closed, here sap is really n !).
4. Lock Scope
The SAP lock only works for a specific program. Assume that program a changes the 1st records of Table 1, and program B also changes the 1st records of table 1, program a locks the 1st records of Table 1 as soon as it is run, and program B does not lock it. Program B can still modify the 1st records of table 1.
The last function of SE11 is to create a Lock Object. For example, to lock the created database table, the name can be "E" + Table Name (this account may not be able to be created enough, find the basis permission ).
After the name is set, click Create, enter the short text (description) of the lock object, and select the name of the table to be locked in the tables option, then, select "E" (dedicated, accumulative) in the lock parameter below. After setting, a lock object is created. In the lock parameter option,All primary keys in the default table are lock parameters. You can delete unnecessary parameters as needed..
Then, in the program, call the call function 'enqueue_ezmatmov 'and call function 'dequeue _ ezmatmov' to lock and unlock the program. The first function is used to lock the program before it operates the database, the other one is to release the lock after the program operates the database. The Code is as follows:
Data: l_mqid like ZMM_MES_MATM_LOG-MQID,
Rochelle sentti like ZMM_MES_MATM_LOG-SENTTI,
Rochelle userid like ZMM_MES_MATM_LOG-USERID,
L_ B ;like ZMM_MES_MATM_LOG-BWART.
Data: l_errmsg (255 ).
Call function 'enqueue _ ezmatmov'
Exporting
Mode_zmm_mes_matm_log = 'E'
Mandt = sy-mandt
Mqid = l_mqid
Sentti = l_sentti
Userid = l_userid
Bits = l_bits
* X_mqid =''
* X_sentti =''
* X_userid =''
* X_bits =''
_ Scope = '2 ′
* _ Wait =''
* _ Collect =''
Exceptions
Foreign_lock = 1
System_failure = 2
Others = 3.
If sy-subrc <> 0.
* Message ID SY-MSGID type SY-MSGTY number SY-MSGNO
* With SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4
* Into l_errmsg.
* Write:/l_errmsg.
Message e001 (z001) with 'multiple users cannot perform operations on data at the same time! '.
Exit.
Endif.
Select zpesinfnr ....
...
Call function 'dequeue _ ezmatmov'
Exporting
Mode_z1_infnr = 'E'
Mandt = sy-mandt
.
Attachment is
Finally, in se37, you can see the locking and unlocking functions automatically generated by the system. For details, refer to enqueue_ezmatmov and dequeue_ezordcfm. the locking and unlocking functions are stored in the function group/1 bcdwben/zen0000.
Delete lock objects in SAP
When you open a transaction in SAP, you cannot open another transaction because of the existence of the lock object, however, there are also ways to delete the Lock Object so that multiple users can operate on one transaction.
T-code: sm12
Code:
Data: lt_enq_del type standard table of seqg3,
Lt_enq_read type standard table of seqg7,
Lw_enq_read type seqg7,
Lw_enq_del type seqg3,
Lv_subrc type sy-subrc.
1. Read all the lock details in System
Call function 'enque _ read2'
Exporting
Gclient = sy-mandt
Gname =''
Guname = '*'
Tables
Enq = lt_enq_read.
2. We will search entry for Table Level Lock for our table
Loop at lt_enq_read into lw_enq_read
Where gname EQ 'rstables'
And Garg CS 'zops _ profile '.
Move-corresponding lw_enq_read to lw_enq_del.
Append lw_enq_del to lt_enq_del.
Endloop.
3. Delete table Level Lock entry for our table
Call function 'enque _ delete'
Exporting
Check_upd_requests = 1
Importing
Subrc = lv_subrc
Tables
Enq = lt_enq_del.
Sap abap considerations for table parameter locking and unlocking Summary (ja-son-7 original)
1. After creating a table in SE11, you may not need to use SE16 to export data in the table. The lock is only because the lock field is not the lock value. If the lock is not indicated, the lock can still be locked, except that all the lock parameters shown in sm12 are ###, that is, the entire table is locked, such ;,
2. One of the two lock List objects displayed in sm12 is the lock on the function after the system enters the system in editing mode. The lock type is X, one is the function lock of the function in the table. The lock type is defined when you create the Lock Object;
3. When locking table parameters, write a write in if endif in the final code to prevent the program from automatically unlocking after execution, if the automatic execution is complete, the program will be automatically unlocked, which will cause the lock list to be invisible after the sm12 is used;
Lock and unlock
When you use a logical lock to lock table entries, the system automatically writes records to the lock table.
When you call the FM to set the lock, if the lock parameters parameter is not specified, the system locks the entire table. Of course, lock parameter: client is a bit special, if not specified, the default is SY-MANDT; if the corresponding client is specified, the corresponding table records on the corresponding client will be locked; if set to space, all clients are locked.
When logical lock settings fail, there are two exceptions. One is exception: foreign_lock, which means the lock has been made; the other is exception: system_failure.
In some cases, logical locks set successfully in the program are implicitly unlocked. For example, when the program ends (when the message type is a or X), use the leave program and leave totransaction statements, or enter/N in the command line and press Enter.
At the end of the program, you can use the dequeue function module to unlock the program (if you do not write this, the program will be automatically unlocked at the end of the program). At this time, the system automatically deletes the corresponding records from the lock table. No exception is generated when dequeue functionmodule is used for unlocking. To unlock all the logical locks you have created in the program, you can use FM: dequeue_all.