[转帖]等待事件 enq:TX - row lock contention分析与解决
6月30日,数据库发生了大量锁表。大概持续1小时,并且越锁越多。后来通过业务人员停掉程序,并kill掉会话后解决。
几天后再EM上查看CPU占用:

CPU发生了明显等待。

主要是由于enq:TX - row lock contention等待事件造成。
等待事件—enq:TX - row lock contention
enq是一种保护共享资源的锁定机制,一个排队机制,先进先出(FIFO)
发生TX锁的原因一般有几个:
1.不同的session更新或删除同一个记录。
2.唯一索引有重复索引
3.位图索引多次更新
4.同时对同一个数据块更新
5.等待索引块分裂
官网上关于TX - row lock contention的内容:
Oracle官网上关于TX - row lock contention的内容
10.3.7.2.4 TX enqueue These are acquired exclusive when a transaction initiates its first change and held until the transaction does a COMMIT or ROLLBACK.
Waits for TX in mode 6: occurs when a session is waiting for a row level lock that is already held by another session. This occurs when one user is updating or deleting a row, which another session wishes to update or delete. This type of TX enqueue wait corresponds to the wait event enq: TX - row lock contention.
The solution is to have the first session already holding the lock perform a COMMIT or ROLLBACK.
Waits for TX in mode 4 can occur if the session is waiting for an ITL (interested transaction list) slot in a block. This happens when the session wants to lock a row in the block but one or more other sessions have rows locked in the same block, and there is no free ITL slot in the block. Usually, Oracle dynamically adds another ITL slot. This may not be possible if there is insufficient free space in the block to add an ITL. If so, the session waits for a slot with a TX enqueue in mode 4. This type of TX enqueue wait corresponds to the wait event enq: TX - allocate ITL entry.
The solution is to increase the number of ITLs available, either by changing the INITRANS or MAXTRANS for the table (either by using an ALTER statement, or by re-creating the table with the higher values).
Waits for TX in mode 4 can also occur if a session is waiting due to potential duplicates in UNIQUE index. If two sessions try to insert the same key value the second session has to wait to see if an ORA-0001 should be raised or not. This type of TX enqueue wait corresponds to the wait event enq: TX - row lock contention.
The solution is to have the first session already holding the lock perform a COMMIT or ROLLBACK.
Waits for TX in mode 4 is also possible if the session is waiting due to shared bitmap index fragment. Bitmap indexes index key values and a range of ROWIDs. Each 'entry' in a bitmap index can cover many rows in the actual table. If two sessions want to update rows covered by the same bitmap index fragment, then the second session waits for the first transaction to either COMMIT or ROLLBACK by waiting for the TX lock in mode 4. This type of TX enqueue wait corresponds to the wait event enq: TX - row lock contention.
Waits for TX in Mode 4 can also occur waiting for a PREPARED transaction.
Waits for TX in mode 4 also occur when a transaction inserting a row in an index has to wait for the end of an index block split being done by another transaction. This type of TX enqueue wait corresponds to the wait event enq: TX - index contention. 10.3.7 enqueue (enq:) waits Enqueues are locks that coordinate access to database resources. This event indicates that the session is waiting for a lock that is held by another session. The name of the enqueue is included as part of the wait event name, in the form enq: enqueue_type - related_details. In some cases, the same enqueue type can be held for different purposes, such as the following related TX types: enq: TX - allocate ITL entry enq: TX - contention enq: TX - index contention enq: TX - row lock contention The V$EVENT_NAME view provides a complete list of all the enq: wait events. You can check the following V$SESSION_WAIT parameter columns for additional information: P1 - Lock TYPE (or name) and MODE P2 - Resource identifier ID1 for the lock P3 - Resource identifier ID2 for the lock
通过awr报告查看:
SQL> @?/rdbms/admin/awrrpt.sql
- 1
1.这2个小时进行AWR的收集和分析,首先,从报告头中看到DB Time达到2176分钟,(DB Time)/Elapsed=18,这个比值偏高:

2.再看TOP 10事件:
看到排在第一位的是enq: TX - row lock contention事件,也就是说系统中在这一个小时里产生了较为严重的行级锁等待事件。

3. 同时,从段的统计信息章节中,也看到下面的信息:

看到row lock waits发生在表上。
通过命令查看
那么,究竟是什么操作导致了这个enq: TX - row lock contention等待事件呢? 查看系统中,当前有哪些会话产生了enq: TX - row lock contention等待事件?
现在已经解锁了,所以无法查询
SQL> select event,sid,p1,p2,p3 from v$session_wait where event='enq: TX - row lock contention';
未选定行
如果正在锁着,可以参考enq: TX - row lock contention等待事件
查看系统中的当前会话,是在哪个对象上产生了enq: TX - row lock contention等待事件?
- 查看引起enq: TX - row lock contention等待事件的object_id对象号
SQL> select ROW_WAIT_OBJ#,ROW_WAIT_FILE#,ROW_WAIT_BLOCK#,ROW_WAIT_ROW# from v$session where event='enq: TX - row lock contention';
那么这个数据库对象为ROW_WAIT_OBJ#的对象究竟是什么呢?
- 查看ROW_WAIT_OBJ#对应的对象名称
SQL> select object_name,object_idfrom dba_objects where object_id=【ROW_WAIT_OBJ#】;
- 通过对象名称查看对象的owner及类型
SQL> select OWNER,OBJECT_NAME,OBJECT_ID,DATA_OBJECT_ID, OBJECT_TYPEfrom dba_objectswhereobject_name='【OBJECT_NAME】';
定位到的结果应该同AWR报告中段统计信息吻合。
通过查询gv$session找到当前的等待事件
SQL> select event,count(*) from gv$session group by event;
EVENT COUNT(*)
SQLNet message from client 3205
wait for unread message on broadcast channel 5
Streams AQ: waiting for messages in the queue 4
ASM background timer 2
ges remote message 2
gcs remote message 8
LNS ASYNC end of log 2
pmon timer 2
rdbms ipc message 60
jobq slave wait 4
smon timer 2
gc cr request 1
Streams AQ: qmn slave idle wait 2
class slave wait 12
SQLNet message to client 1
Space Manager: slave idle wait 9
GCR sleep 2
VKTM Logical Idle Wait 2
Streams AQ: waiting for time management or cleanup tasks 2
Streams AQ: qmn coordinator idle wait 2
PX Deq: Execution Msg 1
PX Deq Credit: send blkd 1
DIAG idle wait 4
PING 2
PX Deq: Execute Reply 1
已选择25行。
因为当前已经没有这个等待事件了,可以查看GV_$ACTIVE_SESSION_HISTORY。
SQL> select SAMPLE_TIME,SESSION_ID,USER_ID,SQL_ID,EVENT,CURRENT_OBJ#,CURRENT_FILE#,CURRENT_BLOCK# from GV_$ACTIVE_SESSION_HISTORY
where event like 'enq: TX%' and SAMPLE_TIME like '30-6月%' and module='JDBC Thin Client' and rownum<=500;
- 1
- 2

结果发现很多的enq: TX - row lock contention等待事件。
定位具体SQL:
- 通过sql_ID字段,查询
SQL> select INST_ID,SQL_TEXT from GV_$SQL where sql_id='f9kdn3mdv252a';
SQL> select * from dba_hist_sqltext where sql_id='f9kdn3mdv252a';
- 查看是哪个用户的SQL
GV_$ACTIVE_SESSION_HISTORY中还有一个USER_ID=54
SQL> select USERNAME,USER_ID,CREATED from dba_users where USER_ID='54' ;
- 查看到底是那个表出现了锁等待
SQL> select * from dba_objects where object_id='183598';
OWNER OBJECT_NAME SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE CREATED LAST_DDL_TIME TIMESTAMP STATUS T G S NAMESPACE EDITION_NAME
------------------------------ ----------------- --------- -------------- ------------ ----------------------- ---------------------- ------------------- ------- - - - ---------- -----------
XX NODETITLEREPORT_TASK_PRJ 183598 183598 TABLE 25-4月 -2014 12:23:53 30-6月 -2020 15:42:53 2014-04-25:12:23:53 VALID N N N 1
解决办法
如果正在等待
1.通过v$session找到BLOCK=1的用户,告知用户提交事务
SQL> select SID,TYPE,ID1,ID2,LMODE,REQUEST,CTIME,BLOCK from V$lock where block=1 or request<>0;
2.通过sid找到pid,kill掉该进程
3.更改sql语句,
SQL> SELECT * FROM QRTZ_LOCKS WHERE LOCK_NAME = :1 FOR UPDATE no wait
加nowait的意思是得到或者得不到,不会等待
一般如果是现网中出现了大量类似的问题,排除人为原因,那么就要检查应用了。
</article>
[转帖]等待事件 enq:TX - row lock contention分析与解决的更多相关文章
- ORACLE AWR结合ASH诊断分析enq: TX - row lock contention
公司用户反馈一系统在14:00~15:00(2016-08-16)这个时间段反应比较慢,于是生成了这个时间段的AWR报告, 如上所示,通过Elapsed Time和DB Time对比分析,可以看出在这 ...
- ORACLE等待事件:enq: TX - row lock contention
enq: TX - row lock contention等待事件,这个是数据库里面一个比较常见的等待事件.enq是enqueue的缩写,它是一种保护共享资源的锁定机制,一个排队机制,先进先出(FIF ...
- enq: TX - row lock contention“等待事件的处理
enq: TX - row lock contention“等待事件的处理 session1: SQL> conn scott/triger Connected. SQL> CRE ...
- 大表建立索引引发enq: TX - row lock contention等待
今天要给一张日志表(6000w数据)建立索引,导致生产系统行锁部分功能卡住 create index idx_tb_cid on tb_login_log(user_id); 开始执行后大概花费了20 ...
- 解决一则enq: TX – row lock contention的性能故障
上周二早上,收到项目组的一封邮件: 早上联代以下时间点用户有反馈EDI导入"假死",我们跟踪了EDI导入服务,服务是正常在跑,可能是处理的慢所以用户感觉是"假死" ...
- enq: TX - row lock contention 参数P1,P2,P3说明
enq: TX - row lock contention三个参数,例如,下面的等待事件 * P1 = name|mode <<<<<<< ...
- AWR之-enq TX - row lock contention的性能故障-转
1 对这一个小时进行AWR的收集和分析,首先,从报告头中看到DB Time达到近500分钟,(DB Time)/Elapsed=8,这个比值偏高: Snap Id Snap Time Sessio ...
- [Oracle] enq: TX - row lock contention 优化案例
依据开发反馈.近期每天早上7:30应用会报警.应用的日志显示数据库连接池满了.新的连接被拒绝. 首先.我做了ASH报告(报告区间:7:25 ~ 7:35),从ASH的等待事件发现enq: TX - r ...
- enq: TX - row lock contention故障处理一则
一个非常easy的问题,之所以让我对这个问题进行总结.一是由于没我想象的简单,在处理的过程中遇到了一些磕磕碰碰,甚至绕了一些弯路.二是引发了我对故障处理时的一些思考. 6月19日,下午5点左右.数据库 ...
- 记一则update 发生enq: TX - row lock contention 的处理方法
根据事后在虚拟机中复现客户现场发生的情况,做一次记录(简化部分过程,原理不变) 客户端1执行update语句 SQL> select * from test; ID NAME --------- ...
随机推荐
- k8s卷管理-2
目录 k8s卷管理-2 pv和pvc pv pv的定义 pvc pvc的定义 pv和pvc的绑定关系 手动指定pv与pvc绑定 pod使用pvc pod挂载pvc 访问模式的区别 k8s卷管理-2 之 ...
- 解读登录双因子认证(MFA)特性背后的TOTP原理
摘要:随着互联网密码泄露事件频发,越来越多的产品开始支持多因子认证(MFA),TOTP则是MFA领域里最普遍的一种实现方式,本文介绍TOTP的原理和华为云的实践经验. 原理 TOTP(Time-Bas ...
- VS Code摸鱼神器,让你快速开发AI模型
摘要:ModelArts VS Code插件一键接入云上开发环境介绍及操作指导 对于习惯于使用本地VS Code IDE的开发者,受限于本地资源,采用本地开发加云上调测的远程开发方式不失为一种更好的选 ...
- 带你掌握java反序列化漏洞及其检测
摘要:在本文中将先介绍java反序列化漏洞的原理,然后在此基础上介绍安全工具如何检测.扫描此类漏洞. 本文分享自华为云社区<java反序列化漏洞及其检测>,作者: alpha1e0. 1 ...
- gis经纬度坐标转换多格式兼容:支持字符串/数组/GeoJSON
格式 let coordinatesStrReg = /((-*[1][0-9]{0,2}|0)(\.[0-9]{1,6})*),\s{0,2}((-*[1-9][0-9]{0,1}|0)(\.[0- ...
- Intellij IDEA 显示 access.log 日志
先配置 SpringBoot 记录 access.log 日志,先让accesslog 显示出来
- 无法停止服务、无法终止 PID 4100 的进程
服务中无法停止服务. 先查出服务对应的进程 Microsoft Windows [版本 10.0.19043.1348] (c) Microsoft Corporation.保留所有权利. C:\Us ...
- JVM HotSpot 可达性分析算法实现细节
本文部分摘自<深入理解 Java 虚拟机第三版> 根节点枚举 在之前关于可达性分析算法的介绍中我们讲过,我们需要先找出可固定作为 GC Roots 的节点,然后沿着引用链去寻找那些无用的垃 ...
- go对mongodb的聚合查询
mongodb的环境搭建参考前面一篇通过mongo-driver使用说明 GO 包管理机制 BSON 介绍 在Go中使用BSON对象构建操作命令 在我们发送查询给数据库之前, 很重要的一点是,理解Go ...
- 如何向已有的项目中添加C/C++代码?
第一步: 我们需要在src/main下面建立一个cpp目录,然后在其中写一个CMakeLists.txt文件和一个cpp文件,直接给出代码: #CMakeLists.txt文件# For more i ...
