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

Oracle | 2016-02-03 07:58

崔华,网名 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,就会产生自我死锁,如下所示:

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

....................................................................................................................................................

本文来自于微信公众号转载文章,若有侵权,请联系小麦苗及时删除

ITPUB BLOG:http://blog.itpub.net/26736162

QQ:642808185 若加QQ请注明您所正在读的文章标题

【版权所有,文章允许转载,但须以链接方式注明源地址,否则追究法律责任】

....................................................................................................................................................

posted @ 2016-04-13 19:49  DB宝  阅读(422)  评论(0编辑  收藏  举报