PL/SQL 包编译时hang住的处理

       最近PL/SQL包在编译时被hang住,起初以为是所依赖的对象被锁住。结果出乎意料之外。下面直接看代码演示。

1、在SQL*Plus下编译包时被hang住		
SQL> alter package bo_syn_data_pkg compile;
alter package bo_syn_data_pkg compile
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation

Elapsed: 00:04:52.65                                  -->强行中断,此时编译时间已经超过4分钟

SQL> alter package bo_syn_data_pkg compile body;      -->编译Body时也被hang住
>alter package bo_syn_data_pkg compile body
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation

Elapsed: 00:06:58.05

SQL> select * from v$mystat where rownum<2;

   SID STATISTIC#      VALUE
------ ---------- ----------
  1056          0          1

Elapsed: 00:00:00.01
SQL> select sid,serial#,username from v$session where sid=1056;

   SID    SERIAL#     Oracle User
------ ---------- ---------------
  1056      57643 GOEX_ADMIN

Elapsed: 00:00:00.01

2、故障分析
-->在session 2中监控,没有任何对象被锁住
SQL> @locks_blocking

no rows selected

-->监控编译的session时发现出现library cache pin事件
SQL> select sid,seq#,event,p3text,wait_class from v$session_wait where event like 'library cache pin';

       SID       SEQ# EVENT                     P3TEXT                                   WAIT_CLASS
---------- ---------- ------------------------- ---------------------------------------- --------------------
      1056         69 library cache pin         100*mode+namespace                       Concurrency

-->来看看library cache pin
-->The library cache pin wait event is associated with library cache concurrency. It occurs when the
-->session tries to pin an object in the library cache to modify or examine it. The session must acquire a
-->pin to make sure that the object is not updated by other sessions at the same time. Oracle posts this
-->event when sessions are compiling or parsing PL/SQL procedures and views.
-->上面的描述即是需要将对象pin到library cache,且此时这个对象没有被其他对象更新或持有。对我们的这个包而言,即此时没有其它对象
-->修改该或者其依赖的对象没有被锁住。而此时出现该等待事件意味着包或其依赖对象一定被其它session所持有。前面的查询没有找到任何
-->锁定对象,看来一定包被其它session所持有。

-->查看当前数据库所有的session的情况
-->发现有一个unknow的session
SQL> @sess_users_active

+----------------------------------------------------+
| Active User Sessions (All)                         |
+----------------------------------------------------+

   SID Serial ID    Status    Oracle User     O/S User  O/S PID Session Program              Terminal   Machine
------ --------- --------- -------------- ------------ -------- -------------------------- ---------- ---------
  1086     59678    ACTIVE     GOEX_ADMIN       oracle   5840   oracle@Dev-DB-04 (J000)      UNKNOWN  Dev-DB-04
  1093     54214    ACTIVE     GOEX_ADMIN       oracle   3847   sqlplus@Dev-DB-04 (TNS V1-      pts/1 Dev-DB-04

-->查询该session运行的SQL语句
-->经验证下面的SQL语句正是所编译包中的一部分
SQL> @sess_query_sql
Enter value for sid: 1086
old   8:   AND s.sid = &&sid
new   8:   AND s.sid = 1086

SQL_TEXT
--------------------------------------------------------------------------------
SELECT BO_SYN_DATA_PKG.GEN_NEW_RECID AS REC_ID, TO_CHAR( GOATOTIMESTAMP, 'yyyymm
dd' ) AS TRADE_DATE, 'DMA' AS TRANS_TYPE, TO_CHAR( GOATOACTIONID ) AS EXEC_KEY,
GOATOGROUPREFNUM AS GRP_REF_NUM, GOATOL1ORDERID AS L1_ORDER_ID, GOATOCLORDID AS
CLORDER_ID, TO_CHAR( GOATOACTION ) AS ACTION, GOATOACTIONSTATUS AS ACTION_STATUS
, GOATOACCNUM AS ACC_NUM, GOATOPLCD AS PL_CD, GOATOTIMESTAMP AS ENTRY_DT, GOATOE
NDTIMESTAMP AS EXEC_TIMESTAMP, GOATOBUYORSELL AS ORDER_SIDE, LTRIM( GOATOSTOCKCO
DE, '0' ) AS STOCK_CD, GOATOORDERQTY AS ORDER_QTY, GOATOORDERTYPE AS ORDER_TYPE,
 GOATOINPUTSOURCE AS ORDER_CHANNEL, GOATOINPUTSOURCE AS INPUTSOURCE, GOATOQTY AS
 TRADED_QTY, GOATOUNITPRICE AS TRADED_PRICE, GOATOUNITPRICE AS ACTUAL_TRADED_PRI
CE, GOATOQTY AS TOTAL_TRADED_QTY, GOATOUNSETTLEDAMT AS UNSETTLED_AMT, GOATOALLOR
NONE AS IS_ALL_OR_NONE, GOATOTIMEINFORCE AS TIME_IN_FORCE, GOATOTRADETYPE AS TRA
DE_TYPE, GOATOTRADEAEID AS AE_ID, 'N' AS IS_INDIRECT_TRADE, SYSDATE AS SYN_TIME,
 NULL AS PROCESS_TIME, NULL AS PROCESS_M

-->进一步观察Session的详细情况
-->发现该session的MODULE为DBMS_SCHEDULER,即为一Oracle job,且ACTION与STATE均有描述
-->由此推论,编译包时的Hang住应该是由该job引起的
SQL> SELECT username
  2        ,command
  3        ,status
  4        ,osuser
  5        ,terminal
  6        ,program
  7        ,module
  8        ,action
  9        ,state
 10  FROM   v$session
 11  WHERE  sid = 1086;

USERNAME      COMMAND STATUS   OSUSER     TERMINAL        PROGRAM         MODULE          ACTION               STATE
---------- ---------- -------- ---------- --------------- --------------- --------------- -------------------- ----------
GOEX_ADMIN          3 ACTIVE   oracle     UNKNOWN         oracle@Dev-DB-0 DBMS_SCHEDULER  STP1_PERFORM_SYNC_DA WAITING
                                                          4 (J000)                        TA

-->查看job中定义的情况,该job正好调用了该包
SQL> select job_name,job_type,enabled,state,job_action from dba_scheduler_jobs where job_name like 'STP1%';

JOB_NAME                       JOB_TYPE         ENABL STATE
------------------------------ ---------------- ----- ----------
JOB_ACTION
------------------------------------------------------------------------------------------------------------------
STP1_PERFORM_SYNC_DATA         PLSQL_BLOCK      TRUE  RUNNING
																															 DECLARE
                                                                  err_num NUMBER;
                                                                  err_msg VARCHAR2(32767);

                                                                BEGIN
                                                                  err_num := NULL;
                                                                  err_msg := NULL;

                                                                  BO_SYN_DATA_PKG.perform_sync_data ( err_num, err_msg );
                                                                  COMMIT;
                                                                END;

-->Author: Robinson Cheng 
-->Blog  : http://blog.csdn.net/robinson_0612

-->下面是该job运行的详细情况
SQL> SELECT job_name
  2        ,session_id
  3        ,slave_process_id sl_pid
  4        ,elapsed_time
  5        ,slave_os_process_id sl_os_id
  6  FROM   dba_scheduler_running_jobs;

JOB_NAME                       SESSION_ID     SL_PID ELAPSED_TIME                   SL_OS_ID
------------------------------ ---------- ---------- ------------------------------ ------------
STP1_PERFORM_SYNC_DATA               1086         20 +009 00:51:17.79               5840
RUN_CHAIN$MY_CHAIN2                                  +075 19:55:03.52
RUN_CHAIN$MY_CHAIN1                                  +075 19:57:45.91

-->ELAPSED_TIME列, Elapsed time since the Scheduler job was started 
-->即该job一直处于运行状态,导致包编译失败

3、解决
-->将job对应的session kill掉
SQL> alter system kill session '1086,59678';
alter system kill session '1086,59678'
*
ERROR at line 1:
ORA-00031: session marked for kill

Elapsed: 00:01:00.03

SQL> SELECT username
  2        ,command
  3        ,status
  4        ,osuser
  5        ,terminal
  6        ,program
  7        ,module
  8        ,action
  9        ,state
 10  FROM   v$session
 11  WHERE  sid = 1086;

USERNAME      COMMAND STATUS   OSUSER     TERMINAL        PROGRAM         MODULE          ACTION               STATE
---------- ---------- -------- ---------- --------------- --------------- --------------- -------------------- ----------
GOEX_ADMIN          3 KILLED   oracle     UNKNOWN         oracle@Dev-DB-0 DBMS_SCHEDULER  STP1_PERFORM_SYNC_DA WAITING
                                                          4 (J000)                        TA

-->再次编译时还是被hang住,应该是session还没有被彻底kill
SQL> alter package bo_syn_data_pkg compile;
alter package bo_syn_data_pkg compile
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation

-->再次kill session
SQL> alter system kill session '1086,59678' immediate;

System altered.

-->此时包编译通过
SQL> alter package bo_syn_data_pkg compile;

Package altered.

Elapsed: 00:00:00.32

SQL> alter package bo_syn_data_pkg compile body;

Package body altered.

Elapsed: 00:00:00.18        

4、总结
-->包编译时被hang住,在排除代码自身编写出错的情形下,应考虑是否有对象或依赖对象被其它session所持有
-->其次,包的编译需要将包pin到library cache,会产生library cahce pin等待事件
-->对于引起异常的session将其kill之后再编译     

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏乐沙弥的世界

使用datapump 导出导入同义词(export and import synonym using datapump)

      对于同义词的备份我们有多种方式来实现,如直接通过脚本生成同义词的创建脚本,或者使用dbms_metadata.get_ddl来提取同义词的定义脚本。...

993
来自专栏杨建荣的学习笔记

生产环境sql语句调优实战第九篇(r3笔记第34天)

生产环境中有一些sql语句是不定时炸弹,不声不响的运行着,可能相关的表很大,运行时间达数小时,甚至数天。 上周在生产环境中发现一条sql语句,运行时间几乎是按照...

3435
来自专栏乐沙弥的世界

使用带dblink方式的datapump迁移Oracle 10g到11g

      对于从Oracle 10g下迁移数据库到Oracle 11g,除了使用RMAN方式之外,我们可以使用带dblink的datapump方式来实现基于逻...

641
来自专栏数据和云

追本溯源:Oracle 只读表空间的探索实践

作者简介 ? 胡中豪 云和恩墨西区交付工程师,多年一线 DBA 经验,曾服务于运营商、电网、政府行业、银行等行业客户;擅长数据库故障处理、性能优化、实施升级 本...

2813
来自专栏乐沙弥的世界

PL/SQL-->UTL_FILE包的使用介绍

    在PL/SQL中,UTL_FILE包提供了文本文件输入和输出互功能。也就是说我们可以通过该包实现从操作系统级别来实现文件读取输入或者是写入到操作系统文件...

761
来自专栏大内老A

谈谈基于SQL Server 的Exception Handling[中篇]

三、TRY CATCH & Return 在上面一节中,我通过RAISERROR重写了创建User的Stored procedure,实际上上面的Stored ...

1846
来自专栏杨建荣的学习笔记

使用awk来解析dump文件 (73天)

dump文件是平时工作中经常碰见的,有时候得到一个dump,但是没有提供一些更多的信息,导入的时候就很可能会有问题。 如果某个用户默认表空间是user,但是du...

4058
来自专栏菩提树下的杨过

mybatis 使用经验小结

一、多数据源问题 主要思路是把dataSource、sqlSesstionFactory、MapperScannerConfigurer在配置中区分开,各Map...

2896
来自专栏乐沙弥的世界

Linux/Unix shell 自动导入Oracle数据库

      使用shell脚本实现对Oracle数据库的监控与管理将大大简化DBA的工作负担,如常见的对实例的监控,监听的监控,告警日志的监控,以及数据库的备份...

1932
来自专栏deed博客

Oracle在Linux下安装

2042

扫码关注云+社区