自相矛盾:一个进程可以自成死锁么?

简介:

640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1

崔华,网名 dbsnake

Oracle ACE Director,ACOUG 核心专家

编辑手记:感谢崔华授权我们独家转载其精品文章,也欢迎大家向“Oracle”社区投稿。在新年前,轻松一点,看看崔华这篇小文,通过一个简单的例子,理解Oracle的自制事务、死锁,建议大家动手去测试、尝试,从而从中学到更多的知识。


有朋友问我:“一个transaction会自我死锁吗?也就是自己锁死了自己”。

很凑巧,半个月前我刚好帮同事处理过这种自我死锁的情况。

 

我们这里来构造一个自我死锁的例子:

select sid from v$mystat 

where rownum<2;

       SID

———-

       362

SQL> create table t1 (id varchar2(10),

amount number(10));

Table created

SQL> insert into t1 values('cuihua',100); 

1 row inserted 

SQL> commit;

Commit complete

SQL> select * from t1;

ID              AMOUNT

———- ———–

cuihua             100

 

SQL> create procedure p_autonomous is

  2  PRAGMA  AUTONOMOUS_TRANSACTION;

  3  begin

  4  update t1 set amount=102

  5  where id='cuihua';

  6  commit;

  7  end;

  8  /

Procedure created

SQL> create procedure p_test is

  2 begin

  3 update t1 set amount=101 where id='cuihua';

  4 p_autonomous;

  5 commit;

  6  end;

  7  /

 

Procedure created 

现在只要我执行上述存储过程p_test,就会产生自我死锁,如下所示:

640?wx_fmt=png&wxfrom=5&wx_lazy=1

此时alert log里会显示:

ORA-00060: Deadlock detected.

 More info in file /u01/app/oracle/admin/ipra/udump/ipra_ora_921828.trc.

 

从上述trace文件里我们可以看到:

640?wx_fmt=png&wxfrom=5&wx_lazy=1

也就是说这里的Blocker是session 362,Waiter也是session 362,典型的自己锁死了自己。

不知道我为什么要这样构造的朋友们看了如下这样一段话就什么都明白了:

The Oracle server provides the ability to temporarily suspend a current transaction and begin another. This second transaction is known as an autonomous transaction and runs independently of its parent. The autonomous or child transaction can commit or rollback as applicable, with the execution of the parent transaction being resumed upon its completion.

The parent may then perform further operations and commit or roll back without affecting the outcome of any operations performed within the child. The child transaction does not inherit transaction context (that is, SET TRANSACTION statements). The transactions are organized as a stack: Only the “top” transaction is accessible at any given time. Once completed, the autonomous transaction is “popped” and the calling transaction is again visible. The limit to the number of suspended transactions is governed by the initialization parameter TRANSACTIONS.

The Oracle server uses similar functionality internally in recursive transactions.

Transactions must be explicitly committed or rolled back or an error ORA-6519 is signaled when attempting to return from the autonomous block.

A deadlock situation may occur where a called and calling transaction deadlock; — this is not prevented, but is signaled by an error unique to this situation. The application developer is responsible for avoiding this situation.


本文出自数据和云公众号,原文链接


相关文章
|
2月前
|
消息中间件 安全 算法
死锁和进程间通信
死锁和进程间通信
27 0
|
9月前
|
Arthas Java 测试技术
【线上问题排查】死锁和僵尸进程排查
【线上问题排查】死锁和僵尸进程排查
150 1
|
9月前
|
消息中间件 缓存 算法
死锁和进程通信
死锁和进程通信
60 0
|
安全 算法 调度
411操作系统学习笔记——进程与线程、处理机调度、同步与互斥(PV操作)、死锁(四)
411操作系统学习笔记——进程与线程、处理机调度、同步与互斥(PV操作)、死锁
138 1
411操作系统学习笔记——进程与线程、处理机调度、同步与互斥(PV操作)、死锁(四)
|
算法 调度 C++
410操作系统学习笔记——进程与线程、处理机调度、同步与互斥(PV操作)、死锁(三)
410操作系统学习笔记——进程与线程、处理机调度、同步与互斥(PV操作)、死锁
181 1
410操作系统学习笔记——进程与线程、处理机调度、同步与互斥(PV操作)、死锁(三)
|
算法 调度
409操作系统学习笔记——进程与线程、处理机调度、同步与互斥(PV操作)、死锁(二)
409操作系统学习笔记——进程与线程、处理机调度、同步与互斥(PV操作)、死锁
227 1
409操作系统学习笔记——进程与线程、处理机调度、同步与互斥(PV操作)、死锁(二)
|
存储 消息中间件 程序员
408操作系统学习笔记——进程与线程、处理机调度、同步与互斥(PV操作)、死锁(一)
408操作系统学习笔记——进程与线程、处理机调度、同步与互斥(PV操作)、死锁
349 1
408操作系统学习笔记——进程与线程、处理机调度、同步与互斥(PV操作)、死锁(一)
|
消息中间件 算法 安全
【操作系统】第十一章死锁与进程通信
【操作系统】第十一章死锁与进程通信
143 0
【操作系统】第十一章死锁与进程通信
|
消息中间件 存储 前端开发
前端备战21秋招之操作系统,线程/进程/死锁
前端备战21秋招之操作系统,线程/进程/死锁