如何使用 XA 数据源和事务关闭 JDBC 中的 Oracle DbLinks 以避免 ORA-02020 错误?
我有一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任何分布式 SQL,甚至是 select,都会打开一个事务,必须先关闭该事务,然后才能关闭数据库链接。在调用 ALTER SESSION CLOSE DATABASE LINK 之前,您需要回滚或提交。
但听起来你已经有其他东西来处理你的交易了。如果无法手动回滚或提交,则应尝试增加打开链接的数量。 OPEN_LINKS 参数是最大数量每个会话的链接数。您需要的链接数量并不真正取决于负载,它应该基于不同远程数据库的最大数量。
编辑:
您在评论中描述的情况不应该发生。我对你们的系统了解不够,无法了解交易的实际情况。不管怎样,如果你不能准确地弄清楚系统在做什么,也许你可以用这样的过程替换“alter session close database link”:
你可能需要这个拨款:
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:
You'll probably need this grant: