2

How do I create a timeout for this operation: ?

def db = Sql.newInstance("jdbc:mysql://${mysql_host}:3306/${dbName}", 
    user, pass, 'com.mysql.jdbc.Driver')
db.eachRow(query) { row ->
  // do something with the row
}
ripper234
  • 222,824
  • 274
  • 634
  • 905
  • Similar question can be found [here](http://groovy.329449.n5.nabble.com/SQL-set-timeout-for-query-td372263.html). – dmahapatro Sep 08 '13 at 18:08

2 Answers2

9

I believe the correct way would be something like this:

sql = Sql.newInstance("jdbc:oracle:thin:@localhost:1521:XE", "user", 
                  "pwd", "oracle.jdbc.driver.OracleDriver") 

sql.withStatement { 
   stmt -> stmt.queryTimeout = 10 
} 

sql.eachRow("select * from someTable", { 
 println it 
} ) 

of course, this is where I'd used Oracle, but I hope this can give you an idea.

VictorCreator
  • 724
  • 1
  • 7
  • 17
0

I believe there might not be a general answer, but rather a database/driver-specific answer via parameters to the connection URL.

E.g. for mysql, I think that adding connectTimeout=something&socketTimeout=something might do the trick.

ripper234
  • 222,824
  • 274
  • 634
  • 905