Shared lock "s lock"
Also called a read lock, if the transaction T with the data object a plus s lock, then the transaction T can read a but cannot modify a, the other transaction can only a plus s lock, and cannot add x lock, until T release S lock on A. This ensures that other transactions can read a, but cannot make any modifications to a before T releases the S lock on a. A shared lock causes a locked object to be shared, which repels an exclusive lock, so that other sessions cannot monopolize the resource's modification ("modify" will first add an exclusive lock to the modified object). However, other shared locks are not excluded, so an object can be combined with a shared lock by multiple sessions.
Exclusive lock "x lock"
Also known as write lock. If the transaction t has an X lock on the data object A, the transaction T can read a or modify a, and the other transaction cannot add any locks to a, until T releases the lock on A. This ensures that the other transaction cannot read and modify a until the lock on the A is released by T. An exclusive lock is well understood and is an exclusive resource. Other sessions are not allowed to share or exclusive locks on the same object. However, other sessions can be read, which also proves that ordinary reading is not locked. If the object being checked is modified, the query operation will be redirected to the corresponding undo block (original data) to ensure the transaction is isolated and read uniformly;
Locks are associated with transactions, and the life cycle of a lock is the start of the transaction to the end of the transaction (either commit or rollback is the end)
Here are some examples:
A row of data in a table is modified by a DML statement, and a transaction begins with the steps behind it:
1. Add a shared lock to this table. This is done to prevent other sessions from modifying the table structure of this table through DDL statements. DDL statements to modify this table, you must add an exclusive lock to the table. But now to add a shared lock on the table, it also excludes the DDL to add exclusive lock;
2. Add an exclusive lock to the modified line, and the other session cannot modify the line. But I add a shared lock to the whole table instead of an exclusive lock, so other sessions can still modify the other rows (also go through 1, 22 steps)
There are two points to add:
1. The meaning of locks is to protect objects, so they have a common purpose, which is not to allow other sessions to modify my lock object itself. is the table structure for a table, which is the data for each field for the row.
2. The locked object can be a table or a row inside the table (the entire row, the exclusive lock on the row, all the data in the field are not allowed to move).
Reference:
Http://www.itpub.net/thread-1829284-1-1.html
Shared lock (S lock) and exclusive lock (x Lock)