Script: Who’s using a database link?(找出谁在使用dblink)

Script: Who’s using a database link?(找出谁在使用dblink)

Every once in a while it is useful to find out which sessions are using a database link in an Oracle database. It’s one of those things that you may not need very often, but when you do need it, it is usually rather important.

Yong Huang includes this script on his website, and notes that Mark further attributed author ship in Metalink Forum thread 524821.994. but this note is no longer available.

Here’s the script, complete with comments.

— for 9I and below

-- who is querying via dblink?

-- Courtesy of Tom Kyte, via Mark Bobak

-- this script can be used at both ends of the database link

-- to match up which session on the remote database started

-- the local transaction

-- the GTXID will match for those sessions

-- just run the script on both databases

Select /*+ ORDERED */

substr(s.ksusemnm,1,10)||'-'|| substr(s.ksusepid,1,10)      "ORIGIN",

substr(g.K2GTITID_ORA,1,35) "GTXID",

substr(s.indx,1,4)||'.'|| substr(s.ksuseser,1,5) "LSESSION" ,

s2.username,

substr(

   decode(bitand(ksuseidl,11),

      1,'ACTIVE',

      0, decode( bitand(ksuseflg,4096) , 0,'INACTIVE','CACHED'),

      2,'SNIPED',

      3,'SNIPED',

      'KILLED'

   ),1,10

) "Status",

substr(w.event,1,10) "WAITING"

from  x$k2gte g, x$ktcxb t, x$ksuse s, v$session_wait w, v$session s2

where  g.K2GTDXCB =t.ktcxbxba

and   g.K2GTDSES=t.ktcxbses

and  s.addr=g.K2GTDSES

and  w.sid=s.indx

and s2.sid = w.sid

— for 10g and above

SELECT /*+ ORDERED */

      SUBSTR (s.ksusemnm, 1, 10) || '-' || SUBSTR (s.ksusepid, 1, 10)

          "ORIGIN",

       SUBSTR (g.K2GTITID_ORA, 1, 35) "GTXID",

       SUBSTR (s.indx, 1, 4) || '.' || SUBSTR (s.ksuseser, 1, 5) "LSESSION",

       s2.username,

       SUBSTR (

          DECODE (

             BITAND (ksuseidl, 11),

             1, 'ACTIVE',

             0, DECODE (BITAND (ksuseflg, 4096), 0, 'INACTIVE', 'CACHED'),

             2, 'SNIPED',

             3, 'SNIPED',

             'KILLED'),

          1,

          10)

          "Status",

       SUBSTR (s2.event, 1, 35) "WAITING"

  FROM x$k2gte g,

       x$ktcxb t,

       x$ksuse s,

       v$session s2

 WHERE     g.K2GTDXCB = t.ktcxbxba

       AND g.K2GTDSES = t.ktcxbses

       AND s.addr = g.K2GTDSES

       AND s2.sid = s.indx;

If you want to close a link, issue the following statement, where linkname refers to the name of the link:

sql> commit or rollback;

SQL> alter session close database link &linkname;

Session altered

DBMS_SESSION.CLOSE_DATABASE_LINK

The CLOSE_DATABASE_LINK procedure is used to close an open but inactive database link in the session. The header for the program is,

PROCEDURE DBMS_SESSION.CLOSE_DATABASE_LINK

   (dblink IN VARCHAR2);

Parameter

Description

dblink

Specifies the name of the database link.

Notes

Use of database links establishes a proxy session for the local user on the remote database, and this is a relatively expensive process. This is why Oracle keeps database links open rather than closing them immediately upon completion of the remote operation. Therefore, the CLOSE_DATABASE_LINK procedure probably should not be routinely called, especially for database links that are likely to be referenced again in the current session.

This procedure is equivalent to SQL "ALTER SESSION CLOSE DATABASE LINK ".

Exceptions

The CLOSE_DATABASE_LINK procedure does not raise any package exceptions. It can raise the following Oracle exceptions when the referenced database link cannot be closed:

ORA-02080  Database link is in use.

ORA-02081  Database link is not open.

Restrictions

  • All cursors using the database link must be closed, and any transactions that reference the link (for UPDATE or SELECT purposes) must be ended (with COMMIT or ROLLBACK). 
     
  • The program does not assert a purity level with the RESTRICT_REFERENCES pragma.

Example

To close a database link named LOOPBACK, specify the following:

BEGIN

   DBMS_SESSION.CLOSE_DATABASE_LINK('LOOPBACK');

END;

你可能感兴趣的:(oracle_base,数据库)