ripper234
ripper234

Reputation: 230246

How to configure a timeout for an SQL query in Groovy?

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
}

Upvotes: 2

Views: 2571

Answers (2)

VictorCreator
VictorCreator

Reputation: 744

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.

Upvotes: 10

ripper234
ripper234

Reputation: 230246

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.

Upvotes: 0

Related Questions