Can a trigger be locked; how would one determine that it is?

后端 未结 1 852
忘掉有多难
忘掉有多难 2021-01-13 18:17

In answering Will I miss any changes if I replace an oracle trigger while my application is running?, I went looking to see if the trigger was locked by an INSERT statement.

1条回答
  •  广开言路
    2021-01-13 18:54

    To determine if a trigger(as well as any other stored procedure) is locked or not, the V$ACCESS dynamic performance view can be queried.

    Session #1
    
    insert into test_trigger
     select level
       from dual
    connect by level <= 1000000; 
    
    Session #2
    
    SQL> select *
      2    from v$access
      3   where object = upper('test_trigger_t')
      4  ;
    
    
    Sid  Owner  Object         Type    Con_Id 
    --------------------------------------
    441  HR     TEST_TRIGGER_T TRIGGER  3 
    

    Those kinds of locks are library cache pins(library cache locks are resource(TM type of lock) locks), needed to ensure that an object is protected from being modified while session is executing it.

    --session sid # 441
    insert into test_trigger
      select level
        from dual
     connect by level <= 1000000;
    
    
    -- session sid #24
    create or replace trigger test_trigger_t 
    after insert on test_trigger for each row
    begin
      insert into test_trigger_h (id) values (:new.id);
    end;  
    
    -- Session # 3
    select vs.sid
         , vs.username
         , vw.event
      from v$session       vs
      join v$session_wait  vw
        on (vw.sid = vs.sid)
      join v$access        va
        on (va.owner = vs.username)
     where vs.username = 'HR'
    

    Result:

    Sid Username Event 
    --------------------------
    24 HR library cache pin 
    ....
    441 HR log file switch (checkpoint incomplete) 
    

    Here we can see that the session #441 waits for a log file switching and session #24 waits for library cache pin.

    0 讨论(0)
提交回复
热议问题