Hive JDBC-getConnection失败时的连接泄漏

时间:2019-05-21 08:32:48

标签: java jdbc hive database-connection connection-leaks

我正在使用cloudera配置单元jdbc https://www.cloudera.com/downloads/connectors/hive/jdbc/2-5-4.html

有时,当getConnection()调用失败时(并非总是如此,这取决于服务器的稳定性),它会显示此异常:

MyDAO - Cannot create connection from DataSource@21f421b8 
at com.cloudera.hiveserver2.hivecommon.api.HS2Client.closeSession(Unknown Source)
at com.cloudera.hiveserver2.hivecommon.core.HiveJDBCCommonConnection.establishConnection(Unknown Source)
at com.cloudera.hiveserver2.jdbc.core.LoginTimeoutConnection.connect(Unknown Source)
at com.cloudera.hiveserver2.jdbc.common.BaseConnectionFactory.doConnect(Unknown Source)
at com.cloudera.hiveserver2.jdbc.common.AbstractDataSource.getSimbaConnection(Unknown Source)
at com.cloudera.hiveserver2.jdbc.common.AbstractDataSource.getConnection(Unknown Source)

当我使用netstat cmd进行检查时:

netstat -an --tcp --program

建立了一个新的套接字连接,我必须等待大约1个小时,然后才能看到tcp连接消失了。

问题是:

  1. 为什么当我调用getConnection()时会调用closeSession()?
  2. 是因为closeSession()失败,所以无法释放tcp连接吗?是否认为是连接泄漏?

1 个答案:

答案 0 :(得分:0)

我反编译了驱动程序,请检查H2SClient类:

public void closeSession() throws ErrorException {
    if (this.m_hasOpenSession) {
        try {
            TCloseSessionReq var1 = new TCloseSessionReq();
            var1.setSessionHandle(this.m_openSessionResp.getSessionHandle());
            this.m_client.CloseSession(var1);
            if (null != this.m_client && null != this.m_client.getInputProtocol() && null != this.m_client.getInputProtocol().getTransport()) {
                this.m_client.getInputProtocol().getTransport().close(); //line 8
            }

            this.m_hasOpenSession = false;
        } catch (Exception var3) {
            ErrorException var2 = HiveJDBCCommonDriver.s_HiveMessages.createGeneralException(HiveJDBCMessageKey.CONN_SESSION_ERR.name(), "Close Session Error");
            var2.initCause(var3);
            throw var2;
        }
    }

}

如果有任何异常导致无法到达第8行,则套接字连接不会关闭。 该关闭也应在catch块或finally块中调用:

public void closeSession() throws ErrorException {
    if (this.m_hasOpenSession) {
        try {
            TCloseSessionReq var1 = new TCloseSessionReq();
            var1.setSessionHandle(this.m_openSessionResp.getSessionHandle());
            this.m_client.CloseSession(var1);

            var2.initCause(var3);
            throw var2;
        } finally {
            if (null != this.m_client && null != this.m_client.getInputProtocol() && null != this.m_client.getInputProtocol().getTransport()) {
                this.m_client.getInputProtocol().getTransport().close(); //line 8
            }   
        }
    }
}