像MySQL一样,如何在PostgreSQL中设置“ wait_timeout”值?

时间:2019-05-28 07:45:43

标签: postgresql timeout

我正在使用Postgresql(9.6.3),我需要为Wait_timeout变量设置值。但是我没有找到与此相关的任何答案,也没有找到可以在Postgresql中使用的等效变量,而不是MySQL中的wait_timeout变量。

     long wait_time = 0;
     ResultSet rs = null;
     try {
        Statement st = con.createStatement();
        rs = st.executeQuery("show variables like 'wait_timeout'");
        if(rs.next())
            wait_time = rs.getLong("Value");
        rs.close();
    } catch (SQLException e) {
        logger.error(e);
    }        
// wait time in SQl is maintained in seconds whereas here we need 
milliseconds
     return (wait_time*1000)/2;

执行查询后,resultSet变量中得到空值。我发现了一个名为Statement_timeout的变量,但我不知道它是否等效,因为它可能会影响所有其他会话,而MySQL中的wait_timeout却不会。请给我建议一个更好的解决方案。预先感谢。

1 个答案:

答案 0 :(得分:1)

MySQL实现了很多超时(https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html)。

MySQL具有:

  • wait_timeout-服务器在关闭非交互式连接之前等待活动的秒数。默认值28800s = 8小时。
  • 类似的是“ interactive_timeout”-服务器在关闭交互式连接之前等待其活动的秒数。

PostgreSQL当前具有:

  • “ statement_timeout”-从命令从客户端到达服务器开始,中止所有花费超过指定毫秒数的语句。
  • “ idle_in_transaction_session_timeout”-使用空闲时间长于指定持续时间(以毫秒为单位)的打开事务终止任何会话。

https://www.postgresql.org/docs/11/runtime-config-client.html

在此处查看示例-https://dba.stackexchange.com/questions/164419/is-it-possible-to-limit-timeout-on-postgres-server

说实话,到目前为止,除了“ connect_timeout”外,我不需要摆弄MySQL中的这些超时。所以我不能给你直接的建议。但是也许您需要“ idle_in_transaction_session_timeout”。