当前位置:网站首页>Simulate Oracle lock waiting and manual unlocking

Simulate Oracle lock waiting and manual unlocking

2022-06-11 20:01:00 huryer

simulation Oracle Lock waiting and manual unlocking

1、 purpose

This paper describes in Oracle When the database is locked and waiting , If you view lock information , And how to unlock .

2、 Simulation process

2.1、 Create test table

--  The first 1 Procedures  ---------------------------------------------
--  Create test table 
CREATE TABLE t1(
id INT,
name VARCHAR(10)
)
;

--  Write test data , And submit 
DELETE FROM t1;
insert into t1(id,name) values(1,'a');
insert into t1(id,name) values(2,'b');
commit;

2.2、 Simulate lock waiting


--  The first 1 individual sqlplus Program  ---------------------------------------------
--  View the current session, And update the data :
select userenv('sid') from dual;
update t1 set name = 'a1' where id = 1;

--  The first 2 individual sqlplus Program  ---------------------------------------------
--  Open one more sqlplus Program , perform sql, It's blocked 
select userenv('sid') from dual;
update t1 set name = 'a2' where id = 1;

2.3、 Analysis lock wait

--  The first 3 Procedures  ---------------------------------------------
-- View locking process sql sentence 
select distinct s.sid
    , s.serial#
    , s.blocking_session /* Which is currently being sid Blocking */
    , s.client_info /* Client information (ip)*/
    , s.machine /* Host name */
    , s.osuser  /* Operating system users */
    , s.program /* Program name */
    , s.seconds_in_wait /* The waiting time ( second )*/
    , s.event /* event */
    , s.status /* state */
    , s.wait_class /* Waiting class */
    , lo.oracle_username /*oracle user */
    , lo.locked_mode /* Lock mode */
    , ao.object_name /* Locked objects */
    , s1.sql_text as sql_current  /* Currently executed sql*/
    , s2.sql_text as sql_previous /* Previous execution sql*/
from v$locked_object lo
    , dba_objects ao
    , v$session s
    left join v$sql s1 on s1.sql_id = s.sql_id
    left join v$sql s2 on s2.sql_id = s.prev_sql_id
where ao.object_id = lo.object_id
    and lo.session_id = s.sid
;

The lock waiting condition is shown in the following figure :
 Insert picture description here

2.4、 Unlock wait

You can use the following 2 There are three ways to lock
1、 Commit or rollback the 1 Procedures

commit;
rollback;

2、 End the... Manually 1 individual session

alter system kill session 'sid,serial#';

Observe No 2 Programs can continue to execute .

2.5、 Summary

This article is only used to simulate the occurrence and unlocking process of lock waiting . In actual projects , Can pass sql Query lock waiting list , Export problem data , And unlock it in time , Analyze the problem afterwards .

原网站

版权声明
本文为[huryer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111954027545.html