diff --git itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java index d0b66f1..c25e3dd 100644 --- itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java +++ itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java @@ -1928,4 +1928,79 @@ private void execFetchFirst(String sqlStmt, String colName, boolean oneRowOnly) } } + /** This test is to connect to any database without using the command "Use <>" + * 1)connect to default database. + * 2) Create a new DB test_default. + * 3) Connect to test_default database. + * 4) Connect and create table under test_default_test. + * 5) Connect and display all tables. + * 6) Connect to default database and shouldn't find table test_default_test. + * 7) Connect and drop test_default_test. + * 8) drop test_default database. + */ + + @Test + public void testURIDatabaseName() throws Exception{ + + HiveServer2 hiveServer2 = new HiveServer2(); + hiveServer2.init(new HiveConf()); + hiveServer2.start(); + Thread.sleep(3000); + + String jdbcUri = "jdbc:hive2://localhost:10000/default"; + Connection con1 = DriverManager.getConnection(jdbcUri); + String dbName="test_conn_str_default_db"; + Statement stmt = con1.createStatement(); + stmt.execute("create database if not exists "+dbName); + stmt.close(); + con1.close(); + + con1 = DriverManager.getConnection("jdbc:hive2://localhost:10000/"+dbName); + stmt = con1.createStatement(); + boolean expected = stmt.execute(" create table test_default_test(x int)"); + stmt.close(); + con1.close(); + + con1 = DriverManager.getConnection("jdbc:hive2://localhost:10000/"+dbName); + stmt = con1.createStatement(); + ResultSet res = stmt.executeQuery("show tables"); + boolean testTableExists = false; + while (res.next()) { + assertNotNull("table name is null in result set", res.getString(1)); + if ("test_default_test".equalsIgnoreCase(res.getString(1))) { + testTableExists = true; + } + } + assertTrue("table name " + tableName + + " found in SHOW TABLES result set", testTableExists); + stmt.close(); + con1.close(); + + con1 = DriverManager.getConnection("jdbc:hive2://localhost:10000/default"); + stmt = con1.createStatement(); + res = stmt.executeQuery("show tables"); + testTableExists = false; + while (res.next()) { + assertNotNull("table name is null in result set", res.getString(1)); + if ("test_default_test".equalsIgnoreCase(res.getString(1))) { + testTableExists = true; + } + } + + assertFalse("table name " + tableName + + " NOT found in SHOW TABLES result set", testTableExists); + stmt.close(); + con1.close(); + + con1 = DriverManager.getConnection("jdbc:hive2://localhost:10000/"+dbName); + stmt = con1.createStatement(); + stmt.execute("set hive.support.concurrency = false"); + stmt.execute(" drop table test_default_test"); + expected = stmt.execute("DROP DATABASE "+ dbName); + + if(hiveServer2 != null) { + hiveServer2.stop(); + } + } + } diff --git jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java index e420b75..280dcd8 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -140,7 +140,7 @@ public HiveConnection(String uri, Properties info) throws SQLException { // open client session openSession(); - configureConnection(); + configureConnection(connParams.getDbName()); } private void openTransport() throws SQLException { @@ -276,7 +276,7 @@ private void openSession() throws SQLException { isClosed = false; } - private void configureConnection() throws SQLException { + private void configureConnection(String dbName) throws SQLException { // set the hive variable in session state for local mode if (isEmbeddedMode) { if (!hiveVarMap.isEmpty()) { @@ -293,6 +293,9 @@ private void configureConnection() throws SQLException { for (Entry hiveVar : hiveVarMap.entrySet()) { stmt.execute("set hivevar:" + hiveVar.getKey() + "=" + hiveVar.getValue()); } + + if (dbName!=null) + stmt.execute("use "+dbName); stmt.close(); } }