锁模拟:
session1 与 session2同时对表t2的col1=200的列进行更新,但不提交
SQL> create table t1(col1 int);
SQL> create table t2(col1 int);
SQL> insert into t1 values(100);
SQL> insert into t2 values(200);
SQL> commit;
SQL> update t2 set col1=2000 where col1=200;
SQL> update t1 set col1=1000 where col1=100;
SQL> update t2 set col1=3000 where col1=200;(等待无法执行)
1、查询执行时间超过2秒的会话
SQL> select * from (select sess_id,sql_text,datediff(ss,last_recv_time,sysdate)Y_EXETIME,SF_GET_SESSION_SQL(SESS_ID)fullsql,clnt_ip from v$sessions where state='ACTIVE') where Y_EXETIME>=2;
2、查询当前是否存在锁
SQL> select o.name,l.* from v$lock l,sysobjects o where l.table_id=o.id and blocked=1;
3、查询锁等待情况
SQL> select * from v$trxwait;
4、可以查询到217656请求的锁目前被217662占用
分别查询对应trx_id的SQL_TEXT
select trx_id,sql_text from v$sessions where trx_id=217656;
select trx_id,sql_text from v$sessions where trx_id=217662;
5、分别查询217656与217662对应的SQL内容
之后调用存储过程杀掉目前占用锁的会话即可
select SQL_ID,SESS_ID,TRX_ID from v$sql_history where trx_id=217662;
sp_close_session(95677440);
update执行成功,锁被释放
达梦社区地址:https://eco.dameng.com