Oracle中如何检查数据库链接是否有效?

发布于 2024-09-25 17:46:41 字数 186 浏览 1 评论 0原文

我有一个主数据库,仅在总部设置数据,在不同的分支机构有几个数据库。我为每个分支机构服务器创建了一个数据库链接。

在某些情况下,我想查询所有有效链接(因为某些链接可能由于连接问题或其他原因而无效),所以我的问题是如何检查数据库链接是否有效而不会出现连接超时问题。是否有 SQL 语句让 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

铜锣湾横着走 2024-10-02 17:46:42

我不知道您是否设法完成此操作,但我想做类似的事情并检查哪些数据库链接处于活动状态。我在另一个论坛上发现了这个

Select * from v$dblink 

,它只显示活动的数据库链接。同样,只有当您有权访问 v$dblink 时,此操作才有效。

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.

金橙橙 2024-10-02 17:46:41

您可以通过执行以下命令来验证数据库链接:

select * from dual@my_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;

最后,您可以创建表 my_db_links(id, name, status(0,1)) 并更新它:

update 
  my_db_links mdl
set
  mdl.status = is_link_active(mdl.name);

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);
怼怹恏 2024-10-02 17:46:41

我不确定您是否可以创建查询来检查实时数据库链接。
您可以做的一件事是创建一个由后台进程更新的表,其中包含数据库链接列表,并为每个链接提供“上次活着的时间”时间戳

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

ま柒月 2024-10-02 17:46:41

由于不同类别的问题,任何链接都可能出现问题:

  • 链接定义无效:错误
    用户名、密码(如果使用)、服务
    name

  • 远程帐户已锁定

  • 远程数据库配置(例如超出每个用户的会话数)

  • 远程数据库或主机不可用< /p>

  • 网络连接

考虑到这些故障模式不断变化的性质,不可能有一个字典视图(例如)来描述链接的状态。在后台检查的异步进程也有可能过时。您可以做的最轻量级的测试可能是在需要使用代码中的链接之前发出“select sysdate from Dual@remote_db”

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

橘虞初梦 2024-10-02 17:46:41

您可以使用 WITH FUNCTION 并进行简单的检查:

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;

结果您将收到 OK 或错误消息。

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.

动次打次papapa 2024-10-02 17:46:41

您可以编写一个执行 tnsping 的操作系统级别脚本,因为无论如何数据库链接通常都依赖于 tnsnames.ora。

You could write an OS level script that performs a tnsping, since db links usually depend on the tnsnames.ora anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文