简体   繁体   English

Oracle 连接/查询超时

[英]Oracle connection/query timeout

Is it possible to specify connection/query timeout for the Oracle database queries?是否可以为 Oracle 数据库查询指定连接/查询超时? Either on Oracle side or in Oracle's JDBC driver (10.2.0.4)?在 Oracle 端还是在 Oracle 的 JDBC 驱动程序 (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?那么,那个 Java 客户端只是在比方说 2 分钟后返回错误,而不是等到 Oracle 完成查询?

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.如果您在事务上下文中执行查询,JTA 事务监视器的事务超时值将是查询超时的决定因素。 The configuration for this depends from one application server to another.此配置取决于一个应用程序服务器到另一个应用程序服务器。

At an individual query level (in the absence of a JTA transaction monitor), the setQueryTimeout method 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). setQueryTimeout 不可靠,尽管它有效(至少从 J2SE 客户端)。 It works via the JDBC driver performing a full round-trip to the Oracle database server.它通过 JDBC 驱动程序工作,执行到 Oracle 数据库服务器的完整往返。 Then, it is upto the database to halt execution of the query.然后,由数据库来停止查询的执行。 Don't rely on it for time critical applications.不要依赖它来进行时间紧迫的应用程序。

Have a look at Oracle profiles.查看 Oracle 配置文件。 This allows you to specify several limits at the database level.这允许您在数据库级别指定多个限制。 One of them is a maximum CPU time per query.其中之一是每个查询的最大 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 分钟的查询,您可能需要先对查询进行一些调整。

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 用于 jdbc 版本 < 10.1.0.5 oracle.jdbc.ReadTimeout 用于 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.因此,如果您使用的是 10.1.0.5 或更高版本的 JDBC 驱动程序,则 oracle.jdbc.ReadTimeout 是正确的属性。

Setting oracle.jdbc.ReadTimeout helped to timeout the jdbc calls.设置 oracle.jdbc.ReadTimeout 有助于使 jdbc 调用超时。 I have used it in a production spring boot app by specifying datasource properties as below我通过如下指定数据源属性在生产 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.注意:此应用程序早些时候使用的是 tomcat 连接池,并且在为其设置上述属性后,发生了超时,但池无法有效地处理已关闭的连接对象。 Therefore, I switched to hikariCP and got lot better results.因此,我切换到 hikariCP 并获得了更好的结果。 I have made a video simulating the slow jdbc calls which compares the results with tomcat and hikari connection pools while using the ReadTimeout property.我制作了一个模拟慢速 jdbc 调用的视频,该视频在使用 ReadTimeout 属性时将结果与 tomcat 和 hikari 连接池进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM