Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a database link is valid in Oracle?

I have a main database with only setup data at the headquarter and several databases at different branches.I created a database link for each branch server.

In some case I would like to query all the valid links (as some links could be invalid due to connection problems or anything else),so my question is How to check if the database link is valid without getting in Connection timeout problems. Is there a SQL statement to let the oracle main server do that check and return only the valid database links?

like image 389
Khaled Avatar asked Oct 03 '10 11:10

Khaled


People also ask

How do you check DB link is active or not in Oracle?

Any user can query USER_DB_LINKS to determine which database links are available to that user. Only those with additional privileges can use the ALL_DB_LINKS or DBA_DB_LINKS view.

How do I check if Dblink is working?

If application schema's password is not known to DBA. We can verify public database link using select * from dual@public_db_link; How private db links can be verified by a DBA if application schema's password is not known.

How does DB Link work in Oracle?

A database link is a pointer that defines a one-way communication path from an Oracle Database server to another database server. The link pointer is actually defined as an entry in a data dictionary table. To access the link, you must be connected to the local database that contains the data dictionary entry.

Can you alter a database link in Oracle?

Use the ALTER DATABASE LINK statement to modify a fixed-user database link when the password of the connection or authentication user changes. Notes: You cannot use this statement to change the connection or authentication user associated with the database link. To change user , you must re-create the database link.


1 Answers

You can verify db link by executing:

select * from dual@my_db_link;

To can create function that verifies db link:

function is_link_active(
  p_link_name varchar2
) return number is
  v_query_link varchar2(100) := 'select count(*) alive from dual@'||p_link_name;
  type db_link_cur is REF CURSOR;
  cur db_link_cur;
  v_status number;
begin
  open cur FOR v_query_link; 
  loop
    fetch cur INTO v_status; 
    exit when cur%notfound;
    dbms_output.put_line('v_status='||v_status);
    return v_status;
  end loop;
  close cur;
exception when others then
  close cur;
  return 0; 
end is_link_active;

Lastly, you can create table my_db_links(id, name, status(0,1)) and update it:

update 
  my_db_links mdl
set
  mdl.status = is_link_active(mdl.name);
like image 99
Pawel Solarski Avatar answered Oct 13 '22 08:10

Pawel Solarski