Lock (learning notes), learning notes
Row-level locking and table-level locking:
Row-Level Lock:
When you execute, INSERT, UPDATE, DELETE, and select for update statements,
ORACLE will implicitly implement the locking of records. If the transaction is not committed and rolled back, the data lock will be locked until the transaction is committed.
Table-Level Lock:
TABLE-level locking requires you to use the lock table statement to manually LOCK the TABLE.
Syntax
Lock table name | view name, TABLE name | view name... in lock mode [NOWAIT]
NOWAIT is an option. When the view locks a table, if it finds that it has been locked by other transactions, it will not wait.
Lock: Shared lock and exclusive lock.
Locks are divided into the following types:
|
Row share lock |
During the lock period, other transactions are allowed to perform various operations on the table concurrently, but no transaction is allowed to perform exclusive operations on the same table (exclusive locks are prohibited) |
|
Row exclusive lock |
The user is allowed to perform any operation. Unlike the row share lock, it cannot prevent other transactions from performing manual or exclusive operations on the same table. |
|
SHARE lock |
Other transactions can only perform query operations, but cannot modify operations. |
|
Share row exclusive share exclusive lock |
Allows any user to perform query operations, but does not allow other users to use shared locks.
Common applications of shared exclusive locks |
|
EXCLUSIVE: EXCLUSIVE lock |
The transaction locks the table exclusively. Other users are allowed to query the table, but cannot modify or set any locks. |
Deadlock:
Disable Automatic transaction commit
Set autocommit = OFF Cancel automatic processing and enable Transaction Processing
Pessimistic lock:UseFOR UPDATELock data
First session execution
SELECT * FROM myemp WHERE deptno=10 FOR UPDATE;
2nd sessions are also executed
SELECT * FROM myemp WHERE deptno=10 FOR UPDATE;
2nd sessions found, always waiting, not executed
After the first session executes COMMIT or ROLLBACK, Session 2 can be executed.
Optimistic lock:Delay all locks until the update is to be executed.
Use the UPDATE table name set fieldWHERE Field 1 = query status, AND field 2 = query status
Use a shared lock for the emp TABLE ON THE FIRST SESSION
LOCK TABLE emp IN SHARE MODE NOWAIT
Delete all emp data in the second SESSION
DELETE FROM emp;
Because it is a shared lock, the second SESSION should be able to check and cannot be modified, so it has been waiting
Unlock:
Administrator Login
View database locks
SELECT session_id,oracle_username,process FROM v$locked_object;
The SESSION_ID of the deadlock can be found;
Query the V $ session data dictionary
SELECT sid,serial#,username,lockwait,status FROM v$session where sid IN(SESSION_ID);
Serial #
Pass
ALTER SYSTEM KILL SESSION'sid,serial#';
Remove deadlock