Java Oracle 连接/查询超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1388749/
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
Oracle connection/query timeout
提问by Andrey Adamovich
Is it possible to specify connection/query timeout for the Oracle database queries? Either on Oracle side or in Oracle's JDBC driver (10.2.0.4)? So, that Java client just got an error back after, let's say, 2 minutes instead of waiting until Oracle finishes executing the query?
是否可以为 Oracle 数据库查询指定连接/查询超时?在 Oracle 端还是在 Oracle 的 JDBC 驱动程序 (10.2.0.4) 中?那么,该 Java 客户端只是在 2 分钟后返回错误,而不是等待 Oracle 完成查询?
采纳答案by Vineet Reynolds
If you are executing the query in the context of a transaction, the transaction timeout value of the JTA transaction monitor will be the determinant to query timeout. The configuration for this depends from one application server to another.
如果在事务上下文中执行查询,JTA 事务监视器的事务超时值将是查询超时的决定因素。此配置取决于从一个应用程序服务器到另一个应用程序服务器。
At an individual query level (in the absence of a JTA transaction monitor), the setQueryTimeoutmethod can be used to set the timeout on the execution of a Statement/PreparedStatement/CallableStatement object.
在单个查询级别(在没有 JTA 事务监视器的情况下),setQueryTimeout方法可用于设置 Statement/PreparedStatement/CallableStatement 对象执行的超时时间。
Update
更新
setQueryTimeout is not to be relied on, although it works (atleast from a J2SE client). It works via the JDBC driver performing a full round-trip to the Oracle database server. Then, it is upto the database to halt execution of the query. Don't rely on it for time critical applications.
尽管 setQueryTimeout 有效(至少来自 J2SE 客户端),但它并不值得依赖。它通过 JDBC 驱动程序执行到 Oracle 数据库服务器的完整往返工作。然后,由数据库来停止执行查询。对于时间关键的应用程序,不要依赖它。
回答by Rene
Have a look at Oracle profiles. This allows you to specify several limits at the database level. One of them is a maximum CPU time per query.
看看 Oracle 配置文件。这允许您在数据库级别指定多个限制。其中之一是每个查询的最大 CPU 时间。
If you have queries running for more than 2 minutes on a regular basis you might want to do some tuning of your queries first.
如果您的查询定期运行超过 2 分钟,您可能需要先对查询进行一些调整。
回答by Srini Karthikeyan
According to http://www.javamonamour.org/2012/09/oraclenetconnecttimeout.html
根据http://www.javamonamour.org/2012/09/oraclenetconnecttimeout.html
oracle.net.READ_TIMEOUT for jdbc versions < 10.1.0.5 oracle.jdbc.ReadTimeout for jdbc versions >=10.1.0.5
oracle.net.READ_TIMEOUT for jdbc 版本 < 10.1.0.5 oracle.jdbc.ReadTimeout for jdbc 版本 >=10.1.0.5
So if you are using a JDBC driver version 10.1.0.5 or higher, then oracle.jdbc.ReadTimeout is the correct property.
因此,如果您使用 JDBC 驱动程序版本 10.1.0.5 或更高版本,那么 oracle.jdbc.ReadTimeout 是正确的属性。
回答by Vaibhaw K
Setting oracle.jdbc.ReadTimeout helped to timeout the jdbc calls. I have used it in a production spring boot app by specifying datasource properties as below
设置 oracle.jdbc.ReadTimeout 有助于使 jdbc 调用超时。我通过指定如下数据源属性在生产 Spring Boot 应用程序中使用它
spring.datasource.hikari.connection-timeout=1000
spring.datasource.hikari.dataSourceProperties=oracle.jdbc.ReadTimeout=2000
Note:Earlier this app was using tomcat connection pool and after setting the above properties for the same, timeouts were happening but the pool was not able to handle the closed connection objects efficiently. Therefore, I switched to hikariCP and got lot better results. I have made a videosimulating the slow jdbc calls which compares the results with tomcat and hikari connection pools while using the ReadTimeout property.
注意:之前这个应用程序使用的是 tomcat 连接池,并且在为它设置了上述属性后,超时发生但池无法有效处理关闭的连接对象。因此,我切换到 hikariCP 并获得了更好的结果。我制作了一个模拟慢速 jdbc 调用的视频,该视频在使用 ReadTimeout 属性时将结果与 tomcat 和 hikari 连接池进行了比较。