Oracle: How to find out if there is a transaction pending?

后端 未结 7 1807
太阳男子
太阳男子 2020-11-28 04:34

I\'m looking for a way to find out if there are uncommited INSERT, UPDATE or DELETE statements in the current session. One way would be to check v$lock with the current sid,

相关标签:
7条回答
  • 2020-11-28 05:06
    SELECT * FROM V$TRANSACTION
    WHERE STATUS='ACTIVE';
    

    See: http://forums.oracle.com/forums/thread.jspa?threadID=691061

    0 讨论(0)
  • 2020-11-28 05:08

    This is the query I normally use,

    select s.sid
          ,s.serial#
          ,s.username
          ,s.machine
          ,s.status
          ,s.lockwait
          ,t.used_ublk
          ,t.used_urec
          ,t.start_time
    from v$transaction t
    inner join v$session s on t.addr = s.taddr;
    
    0 讨论(0)
  • 2020-11-28 05:10

    you can check if your session has a row in V$TRANSACTION (obviously that requires read privilege on this view):

    SQL> SELECT COUNT(*)
           FROM v$transaction t, v$session s, v$mystat m
          WHERE t.ses_addr = s.saddr
            AND s.sid = m.sid
            AND ROWNUM = 1;
    
      COUNT(*)
    ----------
             0
    
    SQL> insert into a values (1);
    
    1 row inserted
    
    SQL> SELECT COUNT(*)
           FROM v$transaction t, v$session s, v$mystat m
          WHERE t.ses_addr = s.saddr
            AND s.sid = m.sid
            AND ROWNUM = 1;
    
      COUNT(*)
    ----------
             1
    
    SQL> commit;
    
    Commit complete
    
    SQL> SELECT COUNT(*)
           FROM v$transaction t, v$session s, v$mystat m
          WHERE t.ses_addr = s.saddr
            AND s.sid = m.sid
            AND ROWNUM = 1;
    
      COUNT(*)
    ----------
             0
    
    0 讨论(0)
  • 2020-11-28 05:10

    Use the query below to find out pending transaction.

    If it returns a value, it means there is a pending transaction.

    Here is the query:

    select dbms_transaction.step_id from dual;

    References:
    http://www.acehints.com/2011/07/how-to-check-pending-transaction-in.html http://www.acehints.com/p/site-map.html

    0 讨论(0)
  • 2020-11-28 05:18

    Also see...

    How can I tell if I have uncommitted work in an Oracle transaction?

    0 讨论(0)
  • 2020-11-28 05:26

    The easiest and most reliable solution is to try and start a transaction and see it if succeeds. If some code already started a transaction but has not yet issued any DML, then the V$TRANSACTION view won't show anything.

    In this example below, I handle the exception to raise a user-defined application error. To defer to an existing exception handler, just do a SET TRANSACTION and then immediately COMMIT to undo it.

    DECLARE
        transaction_in_progress EXCEPTION;
        PRAGMA EXCEPTION_INIT(transaction_in_progress, -1453);
    BEGIN
        SET TRANSACTION NAME 'CHECK_FOR_TRANSACTION_ALREADY_SET';
        COMMIT; -- end transaction
    EXCEPTION
        WHEN transaction_in_progress THEN
            RAISE_APPLICATION_ERROR(-20000,'Transaction is already in progress');
    END;
    /
    
    0 讨论(0)
提交回复
热议问题