Optimistic Lock: If a consumer would like to read business object instance data (e.g. the customer invoice instance 3828), he usually needs the current state of this data. It is often not sufficient to just get old buffered data, which might be already changed by a second consumer in a parallel session.
乐观锁:当消费者需要读取BO实例数据时,消费者通常需要当前最新状态的数据。只是简单地将buffer里的数据返回并不能满足该读取请求,因为当前待读取的BO数据可能在一个并行session里已经被其他消费者修改了。
But if a consumer has successfully received the optimistic lock for a certain business object instance, he can relay on the fact, that this data is in the current state.
但如果消费者已经成功获得BO数据上的乐观锁之后,他就可以确认,他读取到的数据是最新的状态。
More than one consumer are able to acquire an optimistic lock for a certain business object instance.
同一时间段内,允许多个消费者获取同一个BO实例上的乐观锁。
Optimistic and exlusive locks are hold until the current transaction is over.
乐观锁和排他锁可以被持有直至当前事务终结。
This is the case if either the current transaction was saved or cleaned up.
事务终结意味着当前事务触发了保存或者清理操作。
This helps to prevent some of the typical deadlock situations.
这种设计避免了死锁。
If the transaction is saved, the optimistic locks are released and the exclusive locks are transformed into optimistic locks.
当事务终结时,乐观锁被释放,排他锁被转换成乐观锁。
‘X’ means that the lock entry was set in ‘shared mode’ and can therefore accept further lock entries by other users also in ‘shared mode’.
X 意味着锁记录在shared mode下被设置,因此接受其他用户再次使用shared mode给当前BO上锁。
There are different ways to synchronize access by several users:
排他锁
Exclusive lock (E): The locked data can be displayed or edited by one user only.
被锁住的数据只能被单一的用户显示或者编辑。
某个BO已经存在排他锁的前提下,不允许其他用户再申请新的排他锁或者共享锁。
Requests for either another exclusive lock or a shared lock are rejected.
共享锁
Shared Lock (S): Several users can access the locked data at the same time in display mode.
多个用户能同时在display mode下显示具有共享锁的数据。
Requests for further shared locks are accepted, even if they are from other users.
持有共享锁的BO数据,可以再接受新的共享锁请求。
Exclusive locks are rejected.
但无法再申请排他锁。
Exclusive but not cumulative lock (X)
While exclusive locks are requested repeatedly by the same transaction and can be reset again in succession
在同一事务里,排他锁可以重复被申请,以及重置。
exclusive but not cumulative locks can only be requested by the same transaction once only. All further lock requests are rejected.
但还存在一种称为排他非累积锁,在一个事务内,只能允许被请求一次,拒绝接下来的所有锁请求。
乐观锁
Optimistic Lock (O): An optimistic lock can be set if the data is displayed in change mode and the lock behaves like a shared lock if there are access collisions.
如果数据在change model下显示时,可以设置乐观锁,乐观锁的表现形式和共享锁一样。
But if the data is changed, the optimistic lock converts to an exclusive lock.
但如果数据发生改变,乐观锁被转换成排他锁。
0ptimistic locks on the object then become invalidated. It can therefore be possible that the conversion fails, if beforehand another optimistic lock was converted.
但这种转换有可能失败,如果之前另一个乐观锁已经提前被转换成了排他锁。
In such cases the use of optimistic locks is only beneficial, if the probability that the data will NOT be changed is high.
======================================================
Depending on the lock mode, when one user has a lock on a record, the lock prevents other users from changing OR EVEN READING that record.
根据锁模式的不同,当一个用户对某条记录持有锁时,该锁可能阻止其他用户编辑,甚至是读取该条记录。
There are three lock modes:
- SHARED
Row-level shared locks allow multiple users to read data, but do not allow any users to change that data.
行级别的共享锁允许多个用户读取数据,但不允许任何用户修改该行数据。
Table-level shared locks allow multiple users to perform read and write operations on the table, but do not allow any users to perform DDL operations.
table级别的共享锁允许多个用户对table执行读写操作,但拒绝DDL操作。
Multiple users can hold shared locks simultaneously.
多个用户可以同时持有共享锁。
- EXCLUSIVE
An exclusive lock allows only one user/connection to update a particular piece of data (insert, update, and delete).
排他锁仅仅允许一个用户/连接去更改数据。
When one user has an exclusive lock on a row or table, no other lock of any type may be placed on it.
当一个用户对行或者表持有排他锁时,拒绝其他任何类型的锁操作。