本文介绍了如何在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=["[email protected]"]
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中设置连接超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:24