【问题标题】:Redshift Connections not closed in proper wayRedshift 连接未以正确方式关闭
【发布时间】:2017-05-04 13:34:02
【问题描述】:

我正在使用 Java 和 Node Js 处理 redshift。不久前发现未在所有关闭连接调用上创建断开事件。 Java(使用 redshift.jdbc41 ):

Connection conn = null;
Statement stmt = null;
try {
       ...
} catch (Exception ex){
    throw new Exception("Can't sync raw data from redshift");
}finally {
    try {
        if (stmt != null) {
            stmt.close();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    try {
        if (conn != null) {
            conn.close();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

和Node Js(使用pg模块)示例:

var client = new pg.Client(conString);
client.connect(function (err) {
    if (err) throw err;
    client.query(query, function (err, result) {
        client.end(function (err) {if (err) throw err;});
        ...
    });
});

所以在执行查询后调用代码关闭连接,但在 stl_connection_log 中为部分连接创建了仅 initiating session 事件,但没有 disconnecting session,因此许多连接保持活跃数天,并且在客户端应用程序停止后不会重置。

有人可以建议如何解决这个问题吗?可能需要为 redshift 或客户端应用程序中的其他工作方式进行一些配置?

【问题讨论】:

  • 您可以在连接字符串中设置 idleTimeoutMillis:。

标签: java node.js amazon-redshift connectivity


【解决方案1】:

你提到的 Node Js 代码对我来说很好用。如果您想通过 redshift 强制关闭连接,可以使用以下查询。

select pg_terminate_backend (pg_backend_pid());

如果我做了一个错误的假设,请发表评论,我会重新调整我的答案。

【讨论】: