Java Oracle 的 JDBC 查询超时是如何实现的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2376615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How is Oracle's JDBC query timeout implemented?
提问by oneself
I was curious as to how the Oralce JDBC thin client implement query timeout. This can be set by calling java.sql.Statement's setQueryTimeout(int seconds) method.
我很好奇 Oralce JDBC 瘦客户端如何实现查询超时。这可以通过调用 java.sql.Statement 的 setQueryTimeout(int seconds) 方法来设置。
Is this implemented in the driver itself on the client side? Is a new thread spawned and joined? Or does the JDBC driver simply send a parameter to Oracle, and then it enforces the timeout?
这是在客户端的驱动程序本身中实现的吗?是否生成并加入了新线程?还是 JDBC 驱动程序只是向 Oracle 发送一个参数,然后强制执行超时?
After the timeout is reached, which resources on the client and database are released, and which hang around? Does Oracle continue to run the query even though the client abandoned it or is it terminated? Is there still a cursor object on the client side?
超时后,客户端和数据库上的哪些资源被释放,哪些挂了?即使客户端放弃或终止,Oracle 是否继续运行查询?客户端还有游标对象吗?
Thank you
谢谢
采纳答案by Gary Myers
Tanel Poder wrote an articleon how a Cancel works through the OCI (Oracle Call Interface). I guess something similar is done for JDBC. If you are using the thick driver, through OCI, you could try tracing the session (through settings sqlnet.ora) and see what gets recorded.
Tanel Poder 写了一篇关于取消如何通过 OCI(Oracle 调用接口)工作的文章。我想 JDBC 也做了类似的事情。如果您使用的是厚驱动程序,通过 OCI,您可以尝试跟踪会话(通过设置 sqlnet.ora)并查看记录的内容。
回答by Kevin
I do know that the query does not continue on the server side when the timeout is reached. There is some intention/signal option sent to the server either before or after the timeout is reached to indicate it the server should stop. I have verified this by looking on the server in various V$ tables to see if the query is running. (V$SESSION, V$SQL, etc)
我知道当达到超时时,查询不会在服务器端继续。在达到超时之前或之后,有一些意图/信号选项发送到服务器,以指示服务器应该停止。我已经通过在各种 V$ 表中查看服务器以查看查询是否正在运行来验证这一点。(V$SESSION、V$SQL 等)
回答by Nivas
When a query actually timesout when using the setTimeOut method, a SQL exception with the Oracle error code ORA-01013 - user requested cancel of current operation
is thrown from the oracle server.
当使用 setTimeOut 方法查询实际超时时,ORA-01013 - user requested cancel of current operation
Oracle 服务器会抛出一个带有 Oracle 错误代码的 SQL 异常。
This would mean that the operation has been cancelled gracefully(as far as oracle is concerned/as much oracle can) - because it is oracle sending this message.
这意味着该操作已被优雅地取消(就 oracle 而言/尽可能多的 oracle) - 因为它是 oracle 发送此消息。
回答by Janek Bogucki
According to Oracle JDBC FAQ
Statement timeout thread. This thread is created if you execute any statement with a timeout. Only one thread is created no matter how many statements or connections.This thread lasts the lifetime of the VM.
语句超时线程。如果您执行任何带有超时的语句,就会创建此线程。无论有多少语句或连接,都只创建一个线程。此线程持续 VM 的生命周期。