如何使用 XA 数据源和事务关闭 JDBC 中的 Oracle DbLinks 以避免 ORA-02020 错误?

发布于 2024-10-18 22:48:18 字数 518 浏览 6 评论 0原文

我有一个基于 JDBC 的应用程序,它使用跨多个连接的 XA 数据源和事务,连接到 Oracle 数据库。应用程序有时需要使用共享 DbLink 与来自另一台 (Oracle) 服务器的表进行联接来进行一些查询。如果我不经常这样做,该请求就会起作用,但是在快速连续发出 4 或 5 个请求后,我会收到错误(ORA-02020 - 使用中的链接太多)。我做了一些研究,建议的补救措施是调用“ALTER SESSION CLOSE DATABASE LINK”。如果我在连接 DbLnk 表的查询之后调用此请求,则会收到错误 ORA-2080(链接正在使用中)。如果我在查询之前调用它,我会得到 ORA-2081(链接已关闭)。这个电话有什么好处吗? JDBC 连接在事务提交(由 servlet 或 EJB 容器管理,具体取决于具体情况)之前就关闭了。我的印象是,当连接关闭时,Oracle 会将链接标记为已关闭,但需要一两分钟才能返回到可用链接池。我知道我可以扩大链接池(使用配置文件中的 open_links 属性),但这并不能保证我在较重的负载下不会遇到同样的问题。我可以做些什么不同的事情来让数据库链接更快地关闭吗?

I have a JDBC-based application which uses XA datasources and transactions which span multiple connections, connected to an Oracle database. The app sometimes needs to make some queries using join with a table from another (Oracle) server using a shared DbLink. The request works if I don't do it too often, but after 4 or 5 requests in rapid succession I get an error (ORA-02020 - too many links in use). I did some research, and the suggested remedy is to call "ALTER SESSION CLOSE DATABASE LINK ". If I call this request after the query that joins the DbLnk table, I get the error ORA-2080 (link is in use). If I call it before the query, I get ORA-2081 (link closed). Does this call do any good at all? The JDBC connection is closed long before the transaction commit (which is managed either by servlet or by EJB container, depending on the circumstances). I get the impression that when the connection closes, Oracle marks the link as closed, but it takes a minute or two for it to return to the pool of available links. I understand I could enlarge the pool of links (using the open_links property in the config file), but that won't guarantee that I won't have the same problem under a heavier load. Is there something I can do differently to get the dblinks to close more rapidly?

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

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

发布评论

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

评论(1

酒中人 2024-10-25 22:48:18

任何分布式 SQL,甚至是 select,都会打开一个事务,必须先关闭该事务,然后才能关闭数据库链接。在调用 ALTER SESSION CLOSE DATABASE LINK 之前,您需要回滚或提交。

但听起来你已经有其他东西来处理你的交易了。如果无法手动回滚或提交,则应尝试增加打开链接的数量。 OPEN_LINKS 参数是最大数量每个会话的链接数。您需要的链接数量并不真正取决于负载,它应该基于不同远程数据库的最大数量。

编辑:

您在评论中描述的情况不应该发生。我对你们的系统了解不够,无法了解交易的实际情况。不管怎样,如果你不能准确地弄清楚系统在做什么,也许你可以用这样的过程替换“alter session close database link”:

create or replace procedure rollback_and_close_db_links authid current_user is
begin
    rollback;
    for links in (select db_link from v$dblink) loop
        execute immediate 'alter session close database link '||links.db_link;
    end loop;
end;
/

你可能需要这个拨款:

grant select on v_$dblink to [relevant user];

Any distributed SQL, even a select, will open a transaction that must be closed before you can close the database link. You need to either rollback or commit before you call ALTER SESSION CLOSE DATABASE LINK.

But it sounds like you've already got something else handling your transactions. If it's not possible to manually rollback or commit, you should try to increase the number of open links. The OPEN_LINKS parameter is the maximum number of links per session. The number of links you need isn't really dependent on the load, it should be based on the maximum number of distinct remote databases.

Edit:

The situation you describe in your comment shouldn't happen. I don't understand enough about your system to know what's really happening with the transactions. Anyway, if you can't figure out exactly what the system is doing maybe you can replace "alter session close database link" with a procedure like this:

create or replace procedure rollback_and_close_db_links authid current_user is
begin
    rollback;
    for links in (select db_link from v$dblink) loop
        execute immediate 'alter session close database link '||links.db_link;
    end loop;
end;
/

You'll probably need this grant:

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