0

我正在使用 cloudera hive jdbc https://www.cloudera.com/downloads/connectors/hive/jdbc/2-6-2.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连接无法释放?是否视为连接泄漏?
4

1 回答 1

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
            }   
        }
    }
}
于 2019-07-05T07:22:28.827 回答