In my Groovy script, I have following structure:

def sql = Sql.newInstance(connString, "user", "password", 
"org.postgresql.Driver")

sql.withTransaction {
     sql.withBatch(){}

     sql.withBatch(){}

     sql.withBatch(){}

     .........

}

sql.close()

I want to take care of timeout issues here.

But Sql API doesn't have any method for it.

So how can I do it? I am using PostgreSQL driver.

I came across this. But I get error:

java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc4.Jdbc4Connection.setNetworkTimeout(Executor, int) is not yet implemented.

PS:

int[] modifyCount = sql.withBatch(batchSize, updateQuery) { ps ->
        keyValue.each { k,v ->
            ps.addBatch(keyvalue:k, newvalue:v)
        }
    }

In above code, when I try to add ps.setQueryTimeout(), error message says no such method defined.

1 Answers

2
daggett On Best Solutions

Low-level timeouts could be defined through connection properties:

https://jdbc.postgresql.org/documentation/head/connect.html

  • loginTimeout Specify how long to wait for establishment of a database connection.
  • connectTimeout The timeout value used for socket connect operations.
  • socketTimeout The timeout value used for socket read operations.

These properties may be specified in either the connection URL or an additional Properties object parameter.

Query timeout. After connecting to the database you could define closure to be executed for each statement:

sql.withStatement{java.sql.Statement stmt->
    stmt.setQueryTimeout( MY_SQL_TIMEOUT )
}