ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

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

2021-04-08 11:02:05  阅读:11  来源: 互联网

标签:自相矛盾 transaction 自成 t1 死锁 commit autonomous cuihua


编辑手记:感谢崔华授权我们独家转载其精品文章,也欢迎大家向“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,就会产生自我死锁,如下所示:

wpsF803.tmp

此时alert log里会显示:

ORA-00060: Deadlock detected.

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

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

wpsF814.tmp

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

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

TheOracle server provides the ability to temporarily suspend a current transaction and begin another. This second transaction is known as an autonomous transactionand 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.

About Me

标签:自相矛盾,transaction,自成,t1,死锁,commit,autonomous,cuihua
来源: https://blog.51cto.com/lhrbest/2692110

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有