What is a deadlock and how does one fix deadlock errors?

本文介绍了Oracle数据库中死锁的发生原因及解决办法。当两个或多个会话互相等待对方锁定的数据时,会发生死锁。Oracle通过回滚其中一个涉及的事务来自动解决死锁问题。文章还讨论了避免多表死锁的方法,并解释了ITL短缺导致的死锁情况。

orafaq

A deadlock occurs when two or more users are waiting for data locked by each other. When this happens, these users are stuck (deadly embraced) and cannot continue processing.

Oracle automatically detects deadlocks and resolves them by rolling back one of the statements involved in the deadlock, thus releasing one set of data locked by that statement. The session that is rolled back will observe Oracle error: ORA-00060: deadlock detected while waiting for resource. Oracle will also write out a trace file with detailed information to the database's UDUMP directory.

Multi-table deadlocks can be avoided by locking tables in same order (in all applications), thus preventing a deadlock condition. For example, session1 lock table: emp then dept; session2: emp then dept. If this is not possible, your application should check for ORA-60 errors and restart the rolled back transactions.

Here is an example of how to simulate a deadlock error:

Session 1 lock table EMP:

SQL> UPDATE emp SET sal=sal+100;
14 rows updated.

Session 2 lock table DEPT:

SQL> UPDATE dept SET loc = 'Japan';
4 rows updated.

Session 1 now update DEPT. The session will hang waiting for a lock (not a deadlock yet!):

SQL> UPDATE dept SET loc = 'Japan';

Session 2 now update EMP, causing the deadlock:

SQL> UPDATE emp SET sal=sal+100;

Oracle will detect the deadlock and roll back one of these statements:

SQL> UPDATE emp SET sal=sal+100;
UPDATE emp SET sal=sal+100
*
ERROR at line 1:
ORA-00060: deadlock detected while waiting for resource

Other more obscure deadlock situations one needs to be aware of:

  • If you get ORA-60 errors on UPDATE and DELETE statements, where two processes wait for 'S' mode locks on each other's 'TX' enqueues, you are experiencing ITL shortage deadlocks". This cannot happen with INSERT statements, as Oracle doesn't wait on ITL (Interested Transaction List) slots for inserts, it will simply try to insert the row into the next available block.

To fix this, recreate the segment with higher INITTRANS and/or PCTFREE values. This will allow more space in the data blocks for Oracle to allocate more transaction entries (24 bytes at a time) when required.

Note: "ITL waits" can be monitored per segments by querying the sys.v_$segment_statistics view.

  • High transaction activity on tables with bitmap indexes. Bitmap indexes are only appropriate in read only/ read mostly environments. You can try to use a very high INITTRANS value for the bitmap index, however, it would be best to disable the index before heavy transactional activity, and to rebuild it when done.
[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10599713/viewspace-1003534/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10599713/viewspace-1003534/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值