SAP lock post topic

Source: Internet
Author: User
Tags lock queue

SAP Lock Mechanism

Http://blog.chinaunix.net/u2/66669/showart_1687418.html

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 FM

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 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.

Mode S: No data needs to be changed, but the data to be displayed is not changed by others.

Mode X: similar to E, but not accumulative, exclusive.

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 _ and dequeue _, which are used to lock and unlock the Lock Object respectively.

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.

**************************************** ************************

Http://blog.csdn.net/chuangxin/archive/2010/04/11/5473494.aspx

I want to add you as a friend.

SAP-like Lock Mechanism Design

1. Lock classification:

1) function lock: The purpose is to limit the number of concurrent calls to a function. For example, if the limit is 2, when the function has been called by two people (not called yet ), when 3rd people call the function again, an error is reported and the function call is blocked to reduce the server load;

2) tablelock)

2. Lock Mechanism

1) KEYWORDS: registercenter, lockqueue, lockentry, lockfactory, and abstractlock)

· Registration Center: sample features of registration and cancellation locks, mainly the application name and lock name of the lock;

· Lock queue: The Registration Center must have a registration record for the lock control unit. When the queue is empty, the lock must be deregistered at the Registration Center;

· Lock Control Unit: The minimum lock control unit, which is equivalent to cells;

· Lock Factory: Create a lock

· Lock: Execute lock, unlock, and determine whether the lock is locked
2) Core class Resolution:

3) locking and unlocking principles

Function locks and table locks differ greatly in the specific content of the lock and unlock operations. Therefore, they are divided into function locks and locks, table locks, and locks.

A) function lock and unlock

Lock:

· Pass in the function lock type to obtain the function Lock Based on the lock factory lockfactory;
· Set lockentry, mainly application, lockname, and maxcount;
· Locking

Pseudocode:

Lockentry inentry = incoming lock Control Unit
Lockentry entry = get entry (inentry) from the registration queue );

If (entry is null ){

Lockentry Le = get entry (inentry) from the registration center );

If (Le is null ){

Inentry. maxcount = default maximum value;

Inentry );

} Else {

Inentry. maxcount = Le. maxcount

}

Inentry. accumlativecount = inentry. accumlativecount + 1;
Inentry );

} Else {

If (entry. accumlativecount> = entry. maxcount ){

An error is reported. If the lock fails, an error is returned.
} Else {

Entry. accumlativecount ++;

Replace the registration Queue (entry );
}

}

· Unlock

Pseudocode:

Lockentry inentry = incoming lock Control Unit
Lockentry entry = get entry (inentry) from the registration queue );

If (entry is null ){

No need to unlock, return results
} Else {

If (entry. accumlativecount> 1 ){

Entry. accumlativecount --;

Replace the registration Queue (entry );
} Else {

Remove the registration Queue (entry );
}

}

B) Table lock and unlock

Lock:

Pseudocode:

Lockentry inentry = incoming lock Control Unit
If (inengty is not registered in the Registration Center ){

Inentry );

}

List = get registration Queue ();

If (list! = NULL ){

If (inentry in list ){

An error is returned.
} Else {

Inentry queue list;

Update list in MC

}

} Else {

Create a queue list and enter the inentry into the queue;

Update list in MC

}

Unlock:

Pseudocode:

Lockentry inentry = incoming lock Control Unit
List = get registration Queue ();

If (list = NULL ){

Inentry is returned;

} Else {

If (inentry in list ){

Inentry team list;

If (list. Size ()

Remove list from Mc;

} Else {

Update list in MC

}

}

}

4) Lock and unlock problems: function locks are not very problematic, mainly because table locks have the following problems.

A) Locking problems: When to lock and when to allocate the lock ID

B) Unlock problems: When to unlock and what locks to unlock

Supplement: common data concurrency and consistency Solutions

1. All field value methods

The core of this method is that when performing the update operation, the where condition matches the front and back values of all fields. The typical application of this method is Pb.

2. timestamp Method

This method is currently the most widely used Optimistic Locking method, that is, using the database mechanism to define a timestamp type field for each table, and then when you perform insert and update operations on the table, the database automatically updates the value of the timestamp field. So long as the user adds a timestamp matching condition when updating the record, Optimistic Locking can be achieved. For example, table timestamp_example (ID int PK, code char (3), TS timestamp)

User A: insert (1, '001'). The database automatically updates the timestamp value, which is assumed to be 0x000000000001;

User B, user C retrieve to get this data (1, '000000', 0x101 );

Then, user B updates the data set code = '002', and the system automatically updates the timestamp. Assume that the updated value is 0x000000000002;

At this time, user C also wants to update this data... Set code = '003 'Where id = 1 and tsequal (TS, 0x000000000001), because the TS field value has been updated during user B's update operation, therefore, user c fails to update, so as to implement Optimistic Locking.

Because the timestamp type is essentially the varbinary type, ing to the Java data type and C # data type is the byte [] type, it is much more difficult to teach basic data types such as int and string in data processing, and timestamp is subject to database constraints. Therefore, if the new platform wants to use this method to implement data concurrency control, it will be changed as follows:

1) Add the soaversion int not null field to all data tables;

2) during the insert operation, soaversion is assigned 1;

3) in the update operation, set soaversion = (soaversion + 1) % 999999. To control data consistency, add soaversion = @ soaversion to the where condition.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/chuangxin/archive/2010/04/11/5473494.aspx

**************************************** *********************************

Simple implementation: (thanks to a Hong)

First, create a lock object under SE11

Call function 'enqueue _ ez_ztablename'
Exporting
Mode_ztablename = 'E'
Mandt = sy-mandt
Keyfield1 = "Value
Keyfield2 = "Value
Keyfield3 = "Value
...
* X_keyfield1 =''
* X_keyfield2 =''
* X_keyfield3 =''
...
* _ Scope = '2'
* _ Wait =''
* _ Collect =''
* If exceptions are not used, message is displayed within FM
Exceptions
Foreign_lock = 1
System_failure = 2
Others = 3.
If sy-subrc 0.
* Retrieve message displayed within function module
Message ID sy-msgid
Type 'I'
Number sy-msgno
With sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
Exit.
Endif.

The following function is used to unlock

Call function 'dequeue _ ez_ztablename'
Exporting
Mode_ztablename = 'E'
Mandt = SY-MANDT
Mandt = sy-mandt
Keyfield1 = "Value
Keyfield2 = "Value
Keyfield3 = "Value
...
* X_keyfield1 =''
* X_keyfield2 =''
* X_keyfield3 =''
...
* _ Scope = '3'
* _ Synchron =''
* _ Collect =''
.

So do I have to write this function moduel by myself?
After the Lock Object is finished, do I write the function module myself?

No, it will generate
To se37.

Dequeue _*

**************************************

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.