FAQ
Author: xuefu
Date: Fri Jan 17 18:07:38 2014
New Revision: 1559206

URL: http://svn.apache.org/r1559206
Log:
HIVE-6180: Hive JDBC Driver connection fail when no default database passed in the connection string (Prasad via Xuefu)

Modified:
     hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java
     hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
     hive/trunk/jdbc/src/java/org/apache/hive/jdbc/Utils.java

Modified: hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java
URL: http://svn.apache.org/viewvc/hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java?rev=1559206&r1=1559205&r2=1559206&view=diff
==============================================================================
--- hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java (original)
+++ hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java Fri Jan 17 18:07:38 2014
@@ -163,5 +163,46 @@ import org.junit.Test;
       stmt.execute(" drop table if exists table_in_non_default_schema");
       expected = stmt.execute("DROP DATABASE "+ dbName);
       stmt.close();
+
+ hs2Conn = DriverManager.getConnection(jdbcUri+"default",System.getProperty("user.name"),"bar");
+ stmt = hs2Conn .createStatement();
+ res = stmt.executeQuery("show tables");
+ testTableExists = false;
+ while (res.next()) {
+ assertNotNull("table name is null in result set", res.getString(1));
+ if (tableInNonDefaultSchema.equalsIgnoreCase(res.getString(1))) {
+ testTableExists = true;
+ }
+ }
+
+ // test URI with no dbName
+ hs2Conn = DriverManager.getConnection(jdbcUri, System.getProperty("user.name"),"bar");
+ verifyCurrentDB("default", hs2Conn);
+ hs2Conn.close();
+
+ hs2Conn = DriverManager.getConnection(jdbcUri + ";", System.getProperty("user.name"),"bar");
+ verifyCurrentDB("default", hs2Conn);
+ hs2Conn.close();
+
+ hs2Conn = DriverManager.getConnection(jdbcUri + ";/foo=bar;foo1=bar1", System.getProperty("user.name"),"bar");
+ verifyCurrentDB("default", hs2Conn);
+ hs2Conn.close();
+ }
+
+ /**
+ * verify that the current db is the one expected. first create table as <db>.tab and then
+ * describe that table to check if <db> is the current database
+ * @param expectedDbName
+ * @param hs2Conn
+ * @throws Exception
+ */
+ private void verifyCurrentDB(String expectedDbName, Connection hs2Conn) throws Exception {
+ String verifyTab = "miniHS2DbVerificationTable";
+ Statement stmt = hs2Conn.createStatement();
+ stmt.execute("DROP TABLE IF EXISTS " + expectedDbName + "." + verifyTab);
+ stmt.execute("CREATE TABLE " + expectedDbName + "." + verifyTab + "(id INT)");
+ stmt.execute("DESCRIBE " + verifyTab);
+ stmt.execute("DROP TABLE IF EXISTS " + expectedDbName + "." + verifyTab);
+ stmt.close();
     }
  }

Modified: hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
URL: http://svn.apache.org/viewvc/hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java?rev=1559206&r1=1559205&r2=1559206&view=diff
==============================================================================
--- hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java (original)
+++ hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java Fri Jan 17 18:07:38 2014
@@ -297,8 +297,10 @@ public class HiveConnection implements j
        for (Entry<String, String> hiveVar : hiveVarMap.entrySet()) {
          stmt.execute("set hivevar:" + hiveVar.getKey() + "=" + hiveVar.getValue());
        }
- if(dbName!=null)
- stmt.execute("use "+dbName);
+ // if the client is setting a non-default db, then switch the database
+ if (!Utils.DEFAULT_DATABASE.equalsIgnoreCase(dbName)) {
+ stmt.execute("use " + dbName);
+ }
        stmt.close();
      }
    }

Modified: hive/trunk/jdbc/src/java/org/apache/hive/jdbc/Utils.java
URL: http://svn.apache.org/viewvc/hive/trunk/jdbc/src/java/org/apache/hive/jdbc/Utils.java?rev=1559206&r1=1559205&r2=1559206&view=diff
==============================================================================
--- hive/trunk/jdbc/src/java/org/apache/hive/jdbc/Utils.java (original)
+++ hive/trunk/jdbc/src/java/org/apache/hive/jdbc/Utils.java Fri Jan 17 18:07:38 2014
@@ -183,17 +183,16 @@ public class Utils {

      // dbname and session settings
      String sessVars = jdbcURI.getPath();
- if ((sessVars == null) || sessVars.isEmpty()) {
- connParams.setDbName(DEFAULT_DATABASE);
- } else {
+ if ((sessVars != null) && !sessVars.isEmpty()) {
+ String dbName = "";
        // removing leading '/' returned by getPath()
        sessVars = sessVars.substring(1);
        if (!sessVars.contains(";")) {
          // only dbname is provided
- connParams.setDbName(sessVars);
+ dbName = sessVars;
        } else {
          // we have dbname followed by session parameters
- connParams.setDbName(sessVars.substring(0, sessVars.indexOf(';')));
+ dbName = sessVars.substring(0, sessVars.indexOf(';'));
          sessVars = sessVars.substring(sessVars.indexOf(';')+1);
          if (sessVars != null) {
            Matcher sessMatcher = pattern.matcher(sessVars);
@@ -202,6 +201,9 @@ public class Utils {
            }
          }
        }
+ if (!dbName.isEmpty()) {
+ connParams.setDbName(dbName);
+ }
      }

      // parse hive conf settings

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcommits @
categorieshive, hadoop
postedJan 17, '14 at 6:08p
activeJan 17, '14 at 6:08p
posts1
users1
websitehive.apache.org

1 user in discussion

Xuefu: 1 post

People

Translate

site design / logo © 2021 Grokbase