锁模拟:
session1 与 session2同时对表t2的col1=200的列进行更新,但不提交
- session1:
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;

- session2:
SQL> update t2 set col1=2000 where col1=200;

- session1:
SQL> update t1 set col1=1000 where col1=100;
SQL> update t2 set col1=3000 where col1=200;(等待无法执行)

- session3:
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);

- session1:

update执行成功,锁被释放
达梦社区地址:https://eco.dameng.com

本文介绍了在数据库中处理并发更新时的锁模拟情况,展示了两个session同时尝试更新同一记录导致的锁等待问题。通过查询v$session、v$lock、v$trxwait视图来检测和分析锁的状况,并提供了查询SQL_ID获取相关会话的SQL内容,以及调用存储过程关闭占用锁的会话。最后,示例中展示了如何解决锁冲突并释放锁。

2287

被折叠的 条评论
为什么被折叠?



