zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

进程 一个 可以 死锁
2023-09-11 14:19:43 时间

640?wx_fmt=jpeg wxfrom=5 wx_lazy=1

崔华,网名 dbsnake

Oracle ACE Director,ACOUG 核心专家

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


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

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

 

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

select sid from v$mystat 

where rownum

       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.


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


线程和进程 / 进程和线程的区别和联系 每个应用程序运行于现代操作系统之上时,操作系统会提供一种抽象,好像系统上只有这个程序在运行,所有的硬件资源都被这个程序在使用。这种假象是通过抽象了一个进程的概念来完成的,进程可以说是计算机科学中最重要和最成功的概念之一.
今天,进程告诉我线程它它它它不想活了(二) 这篇文章我们来探讨它们是如何通信的,进程告诉我说线程不想活了,我不管它死活,我是谁?进程是怎么告诉我的?进程的出现和线程的死亡和我有必然联系吗?文章为你揭露哟...
今天,进程告诉我线程它它它它不想活了(三) 这篇文章我们来探讨它们是如何通信的,进程告诉我说线程不想活了,我不管它死活,我是谁?进程是怎么告诉我的?进程的出现和线程的死亡和我有必然联系吗?文章为你揭露哟...