问题
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?
回答1:
I'm not sure you can create a query to check live db links. One thing you could do is create a table updated by a background process with the list of db links and for each of them a 'last time seen alive' timestamp
回答2:
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);
回答3:
Any link could have a problem due to different categories of issues:
invalid link definition: wrong username, password (if used), service name
remote account locked
remote db configuration (e.g. sessions per user exceeded)
remote db or host unavailability
- network connectivity
Given the changing nature of these failure modes there can't be a dictionary view (for example) that describes the state of the link. An asynchronous process that checks in the background will also stand a chance of being out-of-date. Probably the lightest-weight test you can do is issue a "select sysdate from dual@remote_db" before you need to use the link in your code
回答4:
You could write an OS level script that performs a tnsping, since db links usually depend on the tnsnames.ora anyway.
回答5:
You could use WITH FUNCTION
and do simple check:
WITH FUNCTION check_dblink(p_dblink IN VARCHAR2) RETURN VARCHAR2 IS
r INT;
BEGIN
EXECUTE IMMEDIATE 'SELECT 1 FROM dual@"' || p_dblink || '"' INTO r;
RETURN 'OK';
EXCEPTION
WITH OTHERS THEN
RETURN SQLERRM;
END;
SELECT check_dblink(db_link), udl.*
FROM user_db_links udl;
As result you will get OK
or error message.
回答6:
I don't know if you managed to get this done, but I wanted to do something like this and check which database links are active. I found this on another forum
Select * from v$dblink
which shows only active dblinks. Again, this will work only if you have permission to access v$dblink
.
来源:https://stackoverflow.com/questions/3849621/how-to-check-if-a-database-link-is-valid-in-oracle