1

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.com"]
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 version: 1.8.6

Please help

  • 2
    take a look at this answer: http://stackoverflow.com/questions/18685899/how-to-configure-a-timeout-for-an-sql-query-in-groovy `:)` – albciff May 14 '15 at 16:30

1 Answers1

2

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.

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)

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).

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.

Community
  • 1
  • 1
Keegan
  • 11,345
  • 1
  • 25
  • 38
  • in my case using Groovy 3.0.2, Java 11 and ojdbc10-19.3.0.0 setNetworkTimeout() did not like the null, so I had to provide an actual executor as first arg – user1708042 Jul 20 '22 at 13:25