程序包调用报ORA-06508: PL/SQL: 无法找到正在调用的程序单元
背景:
开发人员修改一个包里一个过程,修改成功,并重新编译成功,在程序日志发现调用这个包报错ORA-06508: PL/SQL: 无法找到正在调用的程序单元
-----会话1:
CREATE OR REPLACE PACKAGE SimplePkg AS
v_GlobalVar1 NUMBER := 1;
PROCEDURE UpdateVar;
END SimplePkg;
/
CREATE OR REPLACE PACKAGE BODY SimplePkg AS
PROCEDURE UpdateVar IS
vv number;
xx number;
BEGIN
v_GlobalVar1 := 7;
END UpdateVar;
END SimplePkg;
/
set line 300
col object_name for a30
alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
SELECT owner, object_type, object_name, status,created,last_ddl_time
FROM all_objects
WHERE
object_name in (SELECT name FROM dba_dependencies WHERE OWNER = 'TEST' AND referenced_name='SIMPLEPKG' )
--AND status = 'INVALID'
ORDER BY OWNER;
OWNER OBJECT_TYPE OBJECT_NAME STATUS CREATED LAST_DDL_TIME
------------------------------ ------------------- ------------------------------ ------- ------------------- -------------------
TEST PACKAGE BODY SIMPLEPKG VALID 2022-03-01 18:33:21 2022-03-01 18:33:21
TEST PACKAGE SIMPLEPKG VALID 2022-03-01 18:33:16 2022-03-01 18:33:16
-----会话2:
BEGIN
simplePkg.UpdateVar;
END; /
-----会话1:
CREATE OR REPLACE PACKAGE BODY SimplePkg AS
PROCEDURE UpdateVar IS
vv number;
xx number;
yy number;
BEGIN
v_GlobalVar1 := 7;
END UpdateVar;
END SimplePkg;
/
set line 300
col object_name for a30
alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
SELECT owner, object_type, object_name, status,created,last_ddl_time
FROM all_objects
WHERE
object_name in (SELECT name FROM dba_dependencies WHERE OWNER = 'TEST' AND referenced_name='SIMPLEPKG' )
--AND status = 'INVALID'
ORDER BY OWNER;
OWNER OBJECT_TYPE OBJECT_NAME STATUS CREATED LAST_DDL_TIME
------------------------------ ------------------- ------------------------------ ------- ------------------- -------------------
TEST PACKAGE BODY SIMPLEPKG VALID 2022-03-01 18:33:21 2022-03-01 18:39:29
TEST PACKAGE SIMPLEPKG VALID 2022-03-01 18:33:16 2022-03-01 18:33:16
-----会话2:
SQL> BEGIN
simplePkg.UpdateVar;
END;
/
BEGIN
*
ERROR at line 1:
ORA-04068: existing state of packages has been discarded
ORA-04061: existing state of package body "TEST.SIMPLEPKG" has been invalidated
ORA-04065: not executed, altered or dropped package body "TEST.SIMPLEPKG"
ORA-06508: PL/SQL: could not find program unit being called: "TEST.SIMPLEPKG"
ORA-06512: at line 2
原因:
出现这种情况的原因是因为,对于全局变量,每一个session会生成一个本地copy,如果程序重新编译的话,就会因程序里原变量找不到而丢弃该变量,继而导致这个错误。
也就是说在一个会话中调用程序包package时,会生成package中全局变量的副本,如果在另一个会话中对此package进行编译就会使前一个会话中的副本失效,故而产生错误。
处理:
要想避免这个错误,可以使程序捕获ORA-06508:的错误进行处理,也可以重新初始化会话