Hive JDBC - connection leakage when getConnection fails

416 views Asked by At

I am using cloudera hive jdbc https://www.cloudera.com/downloads/connectors/hive/jdbc/2-6-2.html

Sometimes, when the calling of getConnection() fails (not always, depends on server stability), it shows this exception:

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)

And when I check with netstat cmd:

netstat -an --tcp --program

There's a new socket connection established, I have to wait about 1 hour then see that tcp connection gone.

The question is:

  1. Why when I call getConnection(), the closeSession() is called?
  2. Is it because the closeSession() fails, the tcp connection cannot be released? Is it considered as a connection leakage?
1

There are 1 answers

0
yelliver On

I decompiled the driver, check the H2SClient class:

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;
        }
    }

}

If there is any exception which makes the line 8 cannot be reached, the socket connection is not closed. That closing should be called also in catch block or finally block:

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
            }   
        }
    }
}