尝试重新编译时ORA-04021

时间:2016-06-21 18:52:33

标签: oracle plsql

我正在尝试为plsql实现一个模拟库。 尝试动态重新编译存储的对象时,我遇到错误ORA-04021。

代码是

create or replace function perform return varchar2 is begin return 'zero'; end;
/

create or replace procedure recompile(source in clob)
is
begin
  execute immediate source;
end;
/

create or replace procedure recompile_usage
is
begin
  recompile('create or replace function perform return varchar2 is begin return ''one''; end;');

  dbms_output.put_line(perform);
  --execute immediate 'begin dbms_output.put_line(perform); end;';

  recompile('create or replace function perform return varchar2 is begin return ''two''; end;');
  dbms_output.put_line(perform);

end;
/

exec recompile_usage;

这是输出

Function created.

Procedure created.

Procedure created.

one
BEGIN recompile_usage; END;

*
ERROR at line 1:
ORA-04021: timeout occurred while waiting to lock object
ORA-06512: at "UTP.RECOMPILE", line 4
ORA-06512: at "UTP.RECOMPILE_USAGE", line 9
ORA-06512: at line 1

发生超时大约需要5到10分钟。

我们如何解锁要重新编译的对象?

1 个答案:

答案 0 :(得分:1)

正如评论中所说,完全不可能通过你试图完成这项工作的方式。我建议你应该直接调用RECOMPILE程序而不是recompile_usage,希望在代码片段下面帮助。

set sqlbl on;
set define off;

DECLARE
  lv_sql VARCHAR2(32676);
BEGIN
  RECOMPILE(
  'create or replace function perform return varchar2 is begin return ''one''; end;'
  );
  recompile(
  'create or replace function perform return varchar2 is begin return ''two''; end;'
  );
END;

-----------------------------OUTPUT--------------------------------------------

anonymous block completed

SELECT OBJECT_NAME,OBJECT_TYPE,STATUS FROM ALL_OBJECTS
WHERE object_name = 'PERFORM';


**OBJECT_NAME   OBJECT_TYPE STATUS**
PERFORM           FUNCTION  VALID