Oracle database table is locked how to query and unlock details

This article is reproduced in: How to Query and Unlock a Locked Oracle Database Table - System City Installer Master

1. The reason for locking the table

It may be that modifying the data in the table, forgetting to commit the transaction will cause the table to be locked. In Oracle database operations, we sometimes use lock table queries and operations such as unlocking and killing processes.

2. The code for lock table query has the following form

1
2
select count(*) from v$locked_object;
select * from v$locked_object;

3. Check which table is locked

1
2
3
select b.owner,b.object_name,a.session_id,a.locked_mode
from v$locked_object a,dba_objects b
where b.object_id = a.object_id;
  • OWNER: The owner user of the data table
  • OBJECT_NAME: the locked table name
  • SESSION_ID: session ID
  • LOCKED_MODE: lock level

The lock level is divided into 6 levels:

  • Level 1 locks are: Select Level 2 locks are: Select for update, Lock For Update, Lock Row Share
  • Level 3 locks are: Insert, Update, Delete, Lock Row Exclusive
  • Level 4 locks are: Create Index, Lock Share
  • Level 5 locks are: Lock Share Row Exclusive
  • 6级锁有:Alter table, Drop table, Drop Index, Truncate table, Lock Exclusive

4. Check which session caused it

1
2
3
4
5
select a.OS_USER_NAME, c.owner, c.object_name, b.sid, b.serial#, logon_time
  from v$locked_object a, v$session b, dba_objects c
 where a.session_id = b.sid
   and a.object_id = c.object_id
 order by b.logon_time;   

5. Kill the corresponding process

1 alter system kill session '1025,41';

Need the user to have the authority of the administrator to operate, where 1025 is sid, 41 is serial#

If there is an ora-00031 error, add immediate after it;

1 alter system kill session '1025,41' immediate;

6. How to avoid table lock

The common problem is that the user update operation does not commit the transaction,
so: if the update operation is performed separately, two operation SQLs need to be written, one is the update operation SQL statement, and the other is the commit statement to submit the transaction.

Guess you like

Origin blog.csdn.net/qq_46071165/article/details/130104761