postgresql - 如何在 groovy 中处理 PostgreSQL 超时

标签 postgresql groovy

在我的 Groovy 脚本中,我有以下结构:

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

sql.withTransaction {
     sql.withBatch(){}

     sql.withBatch(){}

     sql.withBatch(){}

     .........

}

sql.close()

我想在这里处理超时问题。

但是Sql API 没有任何方法。

那我该怎么做呢?我正在使用 PostgreSQL 驱动程序。

我遇到了 this .但是我得到错误:

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

附言:

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

在上面的代码中,当我尝试添加 ps.setQueryTimeout() 时,错误消息说没有定义这样的方法。

最佳答案

低级超时可以通过连接属性定义:

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

  • loginTimeout 指定等待建立数据库连接的时间。
  • connectTimeout 用于套接字连接操作的超时值。
  • socketTimeout 用于套接字读取操作的超时值。

这些属性可以在连接 URL 或附加的 Properties 对象参数中指定。

查询超时。连接到数据库后,您可以定义要为每个语句执行的闭包:

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

关于postgresql - 如何在 groovy 中处理 PostgreSQL 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56108609/

相关文章:

regex - Reg.ex 模式匹配在 postgresql 中不起作用

sql - 高效插入,对 Postgres 中的大表进行重复检查

mysql - 将 PostgreSQL 存储过程迁移到 MYSQL

Java\Groovy - java.io.IOException : Authentication failure in simple authenticated HTTP request

java - 如何将任务添加到 gradle 中的主要 'build' 任务

ios - 无限滚动和重复数据

python - 处理数据库断开连接

jenkins - 在 Jenkins 管道中发送松弛消息时 cps 不匹配

java - Groovy 中的系统调用失败

grails - 在Groovy Controller /服务中使用非默认dataSource