Getting start 28 - pessimistic lock
When multiple clients may read the same data or simultaneously update a data, there must be a means of access control, preventing the same data from being modified to cause confusion, the simplest means is to lock the data, When you perform a data read or update, you cannot do anything on the same data when you perform a data read or update. Pessimistic Locking, as indicated by the name, pessimistic identification, other clients will also access the same data, so the data is locked until the operation is completed after the operation is completed. Pessimistic locking usually implemented, dependent on the locking mechanism provided by the system or database itself, Hibernate, we can use Query or criteria's setLockMode () method to set the table or column to lock ( Row) and its lock mode, the lock mode has the following:
LockMode.write: Locked when INSERT or UPDATE, Hibernate automatically gets locks when the save () method.
LockMode.Upgrade: Locks with Select ... for Update.
LockMode.Upgrade_nowait: Locks with Select ... for Update NOWAIT, is used in an Oracle environment.
LockMode.Read: Hibernate will automatically locate when reading records.
LockMode.none: No lock.
You can also specify the lock mode for locking when using the LOAD () or LOCK () of the session (). If the database does not support the specified locked mode, Hibernate selects a suitable lock replacement rather than throwing out an exception (Hibernate reference manual 10.6).