Author: prasadm
Date: Thu Dec 19 06:33:17 2013
New Revision: 1552241
URL: http://svn.apache.org/r1552241
Log:
HIVE-4256: JDBC2 HiveConnection does not use the specified database (Anandha L
Ranganathan via Prasad Mujumdar)
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
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=1552241&r1=1552240&r2=1552241&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
Thu Dec 19 06:33:17 2013
@@ -20,6 +20,9 @@ package org.apache.hive.jdbc;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -30,6 +33,7 @@ import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hive.jdbc.miniHS2.MiniHS2;
import org.junit.After;
+import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -48,11 +52,11 @@ import org.junit.Test;
String dataFileDir = conf.get("test.data.files").replace('\\', '/')
.replace("c:", "");
dataFilePath = new Path(dataFileDir, "kv1.txt");
+ miniHS2.start();
}
@Before
public void setUp() throws Exception {
- miniHS2.start();
hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL(),
System.getProperty("user.name"), "bar");
hs2Conn.createStatement().execute("set hive.support.concurrency =
false");
}
@@ -60,7 +64,12 @@ import org.junit.Test;
@After
public void tearDown() throws Exception {
hs2Conn.close();
- miniHS2.stop();
+ }
+
+ @AfterClass
+ public static void afterTest() throws Exception {
+ if (miniHS2.isStarted())
+ miniHS2.stop();
}
@Test
@@ -83,4 +92,76 @@ import org.junit.Test;
res.close();
stmt.close();
}
+
+
+ /** This test is to connect to any database without using the command
"Use <<DB>>"
+ * 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{
+
+ String jdbcUri = miniHS2.getJdbcURL().substring(0,
miniHS2.getJdbcURL().indexOf("default"));
+
+ hs2Conn=
DriverManager.getConnection(jdbcUri+"default",System.getProperty("user.name"),"bar");
+ String dbName="test_connection_non_default_db";
+ String tableInNonDefaultSchema="table_in_non_default_schema";
+ Statement stmt = hs2Conn.createStatement();
+ stmt.execute("create database if not exists "+dbName);
+ stmt.close();
+ hs2Conn.close();
+
+ hs2Conn =
DriverManager.getConnection(jdbcUri+dbName,System.getProperty("user.name"),"bar");
+ stmt = hs2Conn .createStatement();
+ boolean expected = stmt.execute(" create table "+tableInNonDefaultSchema
+" (x int)");
+ stmt.close();
+ hs2Conn .close();
+
+ hs2Conn =
DriverManager.getConnection(jdbcUri+dbName,System.getProperty("user.name"),"bar");
+ stmt = hs2Conn .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 (tableInNonDefaultSchema.equalsIgnoreCase(res.getString(1))) {
+ testTableExists = true;
+ }
+ }
+ assertTrue("table name "+tableInNonDefaultSchema
+ + " found in SHOW TABLES result set", testTableExists);
+ stmt.close();
+ hs2Conn .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;
+ }
+ }
+
+ assertFalse("table name "+tableInNonDefaultSchema
+ + " NOT found in SHOW TABLES result set", testTableExists);
+ stmt.close();
+ hs2Conn .close();
+
+ hs2Conn =
DriverManager.getConnection(jdbcUri+dbName,System.getProperty("user.name"),"bar");
+ stmt = hs2Conn .createStatement();
+ stmt.execute("set hive.support.concurrency = false");
+ res = stmt.executeQuery("show tables");
+
+ stmt.execute(" drop table if exists table_in_non_default_schema");
+ expected = stmt.execute("DROP DATABASE "+ dbName);
+ 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=1552241&r1=1552240&r2=1552241&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 Thu Dec
19 06:33:17 2013
@@ -142,7 +142,7 @@ public class HiveConnection implements j
// open client session
openSession();
- configureConnection();
+ configureConnection(connParams.getDbName());
}
private void openTransport() throws SQLException {
@@ -276,7 +276,7 @@ public class HiveConnection implements j
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,8 @@ 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);
stmt.close();
}
}