如何在groovy sql中设置连接超时?

编程入门 行业动态 更新时间:2024-10-27 11:26:00
本文介绍了如何在groovy sql中设置连接超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以在groovy sql中设置连接超时,默认连接超时是什么?我在此处中检查了官方文档,但没有得到任何事物.下面是我的代码.

Is there a way to set connection timeout in groovy sql, also what is the default connection timeout? I checked official doc here but did not get anything. Below is my code.

private final def dev_env = [ url:"jdbc:oracle:thin:@//aguat:1521/orcl", user:"ricky", password:"ricky", driver:"oracle.jdbc.OracleDriver" ] def query="select * from emp where email=?" def keys=["ricky@gmail"] def Sql sql = Sql.newInstance(dev_env) def results = [] sql.eachRow(query,keys){row -> def resultMap = [:] row.toRowResult().each {k,v-> resultMap.put(k,v) } results << resultMap }

Groovy版本:1.8.6

Groovy version: 1.8.6

请帮助

推荐答案

Groovy SQL无法控制超时,这取决于您的驱动程序(在您的情况下是Oracle).如果要在查询上设置超时,请查看此答案.

Groovy SQL doesn't control the timeout, that's up to your Driver (Oracle in your case). If you want to set a timeout on a query, have a look at this answer.

如果需要连接级别设置(以便可以将Sql对象重用于多个查询,并且将每个查询都应用了超时),则需要设置自己的连接并将其传递给Groovy的Sql Facade.像这样

If you're wanting a connection level setting (so that you can reuse the Sql object for multiple queries with the timeout applied to each), you need to setup your own connection and pass it to Groovy's Sql facade. Like this

def dev_env = [ url:"jdbc:oracle:thin:@//aguat:1521/orcl", user:"ricky", password:"ricky", driver:"oracle.jdbc.OracleDriver" ] Class.forName(dev_env['driver']) def conn = DriverManager.getConnection(dev_env['url'], dev_env['user'],dev_env['password']) conn.setNetworkTimeout(null, 10000) def sql = new Sql(conn)

请注意setNetworkTimeout()方法是在Java 7中添加的.如果您使用的是Java的旧版本,请查看此答案(如果要避免对Oracle jar的编译依赖性,可以使用"oracle.jdbc.OracleDriver"代替回答的OracleConnection.CONNECTION_PROPERTY_THIN_NET_CONNECT_TIMEOUT字段).

Note the setNetworkTimeout() method was added in Java 7. If you're using an older version of Java, have a look at this answer (you can use "oracle.jdbc.OracleDriver" instead of the OracleConnection.CONNECTION_PROPERTY_THIN_NET_CONNECT_TIMEOUT field that answer mentions if you want to avoid a compile dependency on Oracle jars).

同样,由于Groovy的Sql不会更改或控制任何连接设置,因此默认超时将与Oracle驱动程序的默认超时相同.

Again, because Groovy's Sql doesn't alter or control any of the connection settings, the default timeout will be whatever the default is for Oracle's Driver.

更多推荐

如何在groovy sql中设置连接超时?

本文发布于:2023-10-16 06:12:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496722.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   groovy   sql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!