---
title: "ORA-02049: 超时: 分布式事务处理等待锁"
author: "小杨学JAVA"
date: "2015-04-21T12:30:55Z"
tags: []
categories: ["数据库"]
slug: "ora-02049"
draft: false
lastmod: "2015-04-21T12:30:55Z"
id: 2204441
url: "https://z3sm2012.iteye.com/blog/2204441"
---

转：http://blog.itpub.net/15672129/viewspace-767129/

今晚接到项目经理发来的数据库问题，报错为ORA-02049: 超时: 分布式事务处理等待锁，第一时间感觉是维护的数据库的dblink问题，因为现在维护的这些数据库使用大量dblink，70%以上的查询都使用dblink，查看对于的存储过程发现，在循环处理后没有commit，导致该问题的出现。

 

下面是在测试环境的模拟：

 

连接第一个数据库，查看DISTRIBUTED\_LOCK\_TIMEOUT参数的值为60，创建测试表，并产生一个事务，不提交

 

SQL&gt; show parameter DISTRIBUTED\_LOCK\_TIMEOUT

 

NAME                                 TYPE        VALUE

\------------------------------------ ----------- ------------------------------

distributed\_lock\_timeout             integer     60

 

SQL&gt; create table t\_herman

  2  (id number(10),

  3  name varchar(20));

 

Table created.

 

SQL&gt; insert into t\_herman values(0,'test');

 

1 row created.

 

SQL&gt; commit;

 

Commit complete.

 

SQL&gt; select to\_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

 

TO\_CHAR(SYSDATE,'YY

\-------------------

2013-07-25 20:40:36

 

SQL&gt; update t\_herman set name='abc' where id=0;

 

1 row updated.

 

 

 

连接第二个数据库，创建dblink连接第一个数据库，并修改上面表的对于行的数据，等待60秒，产生ORA-02049错误

 

\[ABPCS01]/oracle#sqlplus system/oracle

 

SQL\*Plus: Release 11.2.0.1.0 Production on Thu Jul 25 20:35:39 2013

 

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

 

 

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

 

SQL&gt; show parameter DISTRIBUTED\_LOCK\_TIMEOUT

 

NAME                                 TYPE        VALUE

\------------------------------------ ----------- ------------------------------

distributed\_lock\_timeout             integer     60

 

SQL&gt; create database link to\_hss217 CONNECT TO system IDENTIFIED BY oracle USING 'HSSDB\_217';

 

Database link created.

 

SQL&gt; update t\_herman@to\_hss217 set name='xyz' where id=0;

update t\_herman@to\_hss217 set name='xyz' where id=0

\*

ERROR at line 1:

ORA-02049: timeout: distributed transaction waiting for lock

ORA-02063: preceding line from TO\_HSS217

 

 

总结：

DISTRIBUTED\_LOCK\_TIMEOUT：specifies the amount of time (in seconds) for distributed transactions to wait for locked resources.

该参数指定等待分布式锁资源的时间，超过这个时间事务自动回滚，即在等待未释放的资源超过一定的时间，系统自动报错给客户端

该值默认为60，即等待分布式锁资源60秒

从上面的测试来看，使用dblink做分布式事务时一定要及时commit或rollback，尽量在分布式应用中使用短事务为宜