diff --git a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/session/HiveSessionImplForTest.java b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/session/HiveSessionImplForTest.java index e69de29..09be8e4 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/session/HiveSessionImplForTest.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/session/HiveSessionImplForTest.java @@ -0,0 +1,39 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hive.service.cli.session; + +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hive.service.cli.thrift.TProtocolVersion; + +public class HiveSessionImplForTest extends HiveSessionImpl { + public HiveSessionImplForTest(TProtocolVersion protocol, String username, + String password, HiveConf serverhiveConf, String ipAddress) { + super(protocol, username, password, serverhiveConf, ipAddress); + } + + @Override + protected synchronized void acquire(boolean userAccess) { + + } + + @Override + protected synchronized void release(boolean userAccess) { + + } +} diff --git a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/session/TestHiveSessionImpl.java b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/session/TestHiveSessionImpl.java index e69de29..80f14f5 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/session/TestHiveSessionImpl.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/session/TestHiveSessionImpl.java @@ -0,0 +1,73 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hive.service.cli.session; + +import junit.framework.TestCase; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hive.service.cli.HiveSQLException; +import org.apache.hive.service.cli.OperationHandle; +import org.apache.hive.service.cli.operation.ExecuteStatementOperation; +import org.apache.hive.service.cli.operation.OperationManager; +import org.apache.hive.service.cli.thrift.TProtocolVersion; +import org.junit.Test; + +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; + +import java.util.HashMap; +import java.util.Map; + +public class TestHiveSessionImpl extends TestCase { + @Test + public void testLeakOperationHandle() throws HiveSQLException { + TProtocolVersion protocol = TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V2; + String username = ""; + String password = ""; + HiveConf serverhiveConf = new HiveConf(); + Map confOverlay = new HashMap(); + String ipAddress = null; + OperationManager operationManager = mock(OperationManager.class); + ExecuteStatementOperation operation = mock(ExecuteStatementOperation.class); + OperationHandle opHandle = mock(OperationHandle.class); + when(operation.getHandle()).thenReturn(opHandle); + HiveSessionImplForTest session = new HiveSessionImplForTest(protocol, username, password, + serverhiveConf, ipAddress); + session.setOperationManager(operationManager); + String statement = "drop table if exists table_not_exists"; + when(operationManager.newExecuteStatementOperation(session, statement, confOverlay, + true)).thenReturn(operation); + try { + //first no HiveSQLException throw,then no need to close opHandle + session.executeStatementAsync(statement, confOverlay); + verify(operationManager, times(0)).closeOperation(opHandle); + statement = "select * from table_not_exists"; + when(operationManager.newExecuteStatementOperation(session, statement, confOverlay, + true)).thenReturn(operation); + doThrow(new HiveSQLException("Fail for clean up test")).when(operation).run(); + session.executeStatementAsync(statement, confOverlay); + fail("HiveSqlException expected."); + } catch (HiveSQLException e) { + if (!"Fail for clean up test".equals(e.getMessage())) { + fail("unexpected failure:" + e.getMessage()); + } + //Catching an HiveSQLException ,then close the opHandle + verify(operationManager, times(1)).closeOperation(opHandle); + } + } +} diff --git a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java index b62cfd9..ccc1f5e 100644 --- a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java +++ b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java @@ -382,11 +382,10 @@ private OperationHandle executeStatementInternal(String statement, Map