diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java index 4a82aa5..5c049f9 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java @@ -30,6 +30,7 @@ import org.apache.hadoop.hive.ql.processors.DfsProcessor; import org.apache.hive.common.util.HiveVersionInfo; import org.apache.hive.jdbc.Utils.JdbcConnectionParams; +import org.apache.hive.service.cli.HiveSQLException; import org.apache.hive.service.cli.operation.ClassicTableTypeMapping; import org.apache.hive.service.cli.operation.ClassicTableTypeMapping.ClassicTableTypes; import org.apache.hive.service.cli.operation.HiveTableTypeMapping; @@ -577,7 +578,7 @@ public void testExecutePreparedStatement() throws Exception { @Test public void testSetOnConnection() throws Exception { - Connection connection = getConnection("test?conf1=conf2;conf3=conf4#var1=var2;var3=var4"); + Connection connection = getConnection(testDbName + "?conf1=conf2;conf3=conf4#var1=var2;var3=var4"); try { verifyConfValue(connection, "conf1", "conf2"); verifyConfValue(connection, "conf3", "conf4"); @@ -2883,4 +2884,10 @@ private void testInsertOverwrite(HiveStatement stmt) throws SQLException { assertEquals(rowCount, dataFileRowCount); stmt.execute("drop table " + tblName); } + + // Test that opening a JDBC connection to a non-existent database throws a HiveSQLException + @Test(expected = HiveSQLException.class) + public void testConnectInvalidDatabase() throws SQLException { + DriverManager.getConnection("jdbc:hive2:///databasedoesnotexist", "", ""); + } } diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java index afe23f8..af58282 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java @@ -112,6 +112,14 @@ public static void setupBeforeClass() throws Exception { stmt.execute("drop database if exists " + testDbName + " cascade"); stmt.execute("create database " + testDbName); stmt.close(); + + try { + openTestConnections(); + } catch (Exception e) { + System.out.println("Unable to open default connections to MiniHS2: " + e); + throw e; + } + // tables in test db createTestTables(conTestDb, testDbName); } @@ -183,6 +191,7 @@ private static void restoreMiniHS2AndConnections() throws Exception { HiveConf conf = new HiveConf(); startMiniHS2(conf); openDefaultConnections(); + openTestConnections(); } private static void startMiniHS2(HiveConf conf) throws Exception { @@ -208,6 +217,9 @@ private static void cleanupMiniHS2() throws IOException { private static void openDefaultConnections() throws Exception { conDefault = getConnection(); + } + + private static void openTestConnections() throws Exception { conTestDb = getConnection(testDbName); } 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 fd74d55..418f453 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 @@ -275,6 +275,13 @@ private void configureSession(Map sessionConfMap) throws HiveSQL throw new HiveSQLException(e); } } else if (key.startsWith("use:")) { + try { + if (sessionHive.getDatabase(entry.getValue()) == null) { + throw new HiveSQLException("Database " + entry.getValue() + " does not exist"); + } + } catch (HiveException e) { + throw new HiveSQLException(e); + } SessionState.get().setCurrentDatabase(entry.getValue()); } else { sessionConf.verifyAndSet(key, entry.getValue());