HIVE-11293 HiveConnection.setAutoCommit(true) throws exception (MichaÅ WÄgrzyn and Alan Gates, reviewed by Thejas Nair)
Project: http://git-wip-us.apache.org/repos/asf/hive/repo Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/1357f633 Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/1357f633 Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/1357f633 Branch: refs/heads/master-fixed Commit: 1357f6338796600fe37b81bb11600ad56da3d4e2 Parents: 1ad1dc8 Author: Alan Gates <ga...@hortonworks.com> Authored: Mon Nov 2 15:53:07 2015 -0800 Committer: Alan Gates <ga...@hortonworks.com> Committed: Mon Nov 2 15:53:07 2015 -0800 ---------------------------------------------------------------------- .../org/apache/hive/jdbc/TestJdbcDriver2.java | 89 ++++++++++++------ .../org/apache/hive/jdbc/HiveConnection.java | 96 +++++++++++--------- 2 files changed, 113 insertions(+), 72 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hive/blob/1357f633/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java ---------------------------------------------------------------------- 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 995a33d..ced454f 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 @@ -18,14 +18,28 @@ package org.apache.hive.jdbc; -import static org.apache.hadoop.hive.conf.SystemVariables.SET_COLUMN_NAME; -import static org.apache.hadoop.hive.ql.exec.ExplainTask.EXPL_COLUMN_NAME; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hive.common.type.HiveIntervalDayTime; +import org.apache.hadoop.hive.common.type.HiveIntervalYearMonth; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.conf.HiveConf.ConfVars; +import org.apache.hadoop.hive.metastore.TableType; +import org.apache.hadoop.hive.ql.exec.UDF; +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.operation.ClassicTableTypeMapping; +import org.apache.hive.service.cli.operation.ClassicTableTypeMapping.ClassicTableTypes; +import org.apache.hive.service.cli.operation.HiveTableTypeMapping; +import org.apache.hive.service.cli.operation.TableTypeMappingFactory.TableTypeMappings; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.InputStream; import java.sql.Connection; @@ -36,6 +50,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; +import java.sql.SQLWarning; import java.sql.Statement; import java.sql.Timestamp; import java.sql.Types; @@ -49,26 +64,14 @@ import java.util.Properties; import java.util.Set; import java.util.regex.Pattern; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.apache.hadoop.fs.Path; -import org.apache.hadoop.hive.common.type.HiveIntervalDayTime; -import org.apache.hadoop.hive.common.type.HiveIntervalYearMonth; -import org.apache.hadoop.hive.conf.HiveConf; -import org.apache.hadoop.hive.conf.HiveConf.ConfVars; -import org.apache.hadoop.hive.metastore.TableType; -import org.apache.hadoop.hive.ql.exec.UDF; -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.operation.ClassicTableTypeMapping; -import org.apache.hive.service.cli.operation.ClassicTableTypeMapping.ClassicTableTypes; -import org.apache.hive.service.cli.operation.HiveTableTypeMapping; -import org.apache.hive.service.cli.operation.TableTypeMappingFactory.TableTypeMappings; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.apache.hadoop.hive.conf.SystemVariables.SET_COLUMN_NAME; +import static org.apache.hadoop.hive.ql.exec.ExplainTask.EXPL_COLUMN_NAME; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** @@ -96,6 +99,8 @@ public class TestJdbcDriver2 { private static boolean standAloneServer = false; private static final float floatCompareDelta = 0.0001f; + @Rule public ExpectedException thrown = ExpectedException.none(); + public TestJdbcDriver2() { conf = new HiveConf(TestJdbcDriver2.class); dataFileDir = conf.get("test.data.files").replace('\\', '/') @@ -2414,4 +2419,32 @@ public void testParseUrlHttpMode() throws SQLException, JdbcUriParseException, } } } + + @Test + public void testAutoCommit() throws Exception { + con.clearWarnings(); + con.setAutoCommit(true); + assertNull(con.getWarnings()); + con.setAutoCommit(false); + SQLWarning warning = con.getWarnings(); + assertNotNull(warning); + assertEquals("Hive does not support autoCommit=false", warning.getMessage()); + assertNull(warning.getNextWarning()); + con.clearWarnings(); + } + + @Test + public void setAutoCommitOnClosedConnection() throws Exception { + Connection mycon = getConnection(""); + try { + mycon.setAutoCommit(true); + mycon.close(); + thrown.expect(SQLException.class); + thrown.expectMessage("Connection is closed"); + mycon.setAutoCommit(true); + } finally { + mycon.close(); + } + + } } http://git-wip-us.apache.org/repos/asf/hive/blob/1357f633/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java ---------------------------------------------------------------------- diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java index 920d50f..e38c585 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -18,48 +18,6 @@ package org.apache.hive.jdbc; -import java.io.FileInputStream; -import java.io.IOException; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; -import java.security.KeyStore; -import java.security.SecureRandom; -import java.sql.Array; -import java.sql.Blob; -import java.sql.CallableStatement; -import java.sql.Clob; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.DriverManager; -import java.sql.NClob; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLClientInfoException; -import java.sql.SQLException; -import java.sql.SQLWarning; -import java.sql.SQLXML; -import java.sql.Savepoint; -import java.sql.Statement; -import java.sql.Struct; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; -import java.util.concurrent.Executor; -import java.util.concurrent.TimeUnit; - -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManagerFactory; -import javax.security.sasl.Sasl; -import javax.security.sasl.SaslException; - -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.protocol.HttpContext; import org.apache.hive.jdbc.Utils.JdbcConnectionParams; import org.apache.hive.service.auth.HiveAuthFactory; import org.apache.hive.service.auth.KerberosSaslHelper; @@ -87,9 +45,11 @@ import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.BasicCookieStore; +import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.BasicHttpClientConnectionManager; +import org.apache.http.protocol.HttpContext; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.transport.THttpClient; @@ -98,6 +58,45 @@ import org.apache.thrift.transport.TTransportException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import javax.security.sasl.Sasl; +import javax.security.sasl.SaslException; +import java.io.FileInputStream; +import java.io.IOException; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.security.KeyStore; +import java.security.SecureRandom; +import java.sql.Array; +import java.sql.Blob; +import java.sql.CallableStatement; +import java.sql.Clob; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.DriverManager; +import java.sql.NClob; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLClientInfoException; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Savepoint; +import java.sql.Statement; +import java.sql.Struct; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.concurrent.Executor; +import java.util.concurrent.TimeUnit; + /** * HiveConnection. * @@ -1216,8 +1215,17 @@ public class HiveConnection implements java.sql.Connection { @Override public void setAutoCommit(boolean autoCommit) throws SQLException { - if (autoCommit) { - throw new SQLException("enabling autocommit is not supported"); + // Per JDBC spec, if the connection is closed a SQLException should be thrown. + if(isClosed) { + throw new SQLException("Connection is closed"); + } + // The auto-commit mode is always enabled for this connection. Per JDBC spec, + // if setAutoCommit is called and the auto-commit mode is not changed, the call is a no-op. + if (!autoCommit) { + LOG.warn("Request to set autoCommit to false; Hive does not support autoCommit=false."); + SQLWarning warning = new SQLWarning("Hive does not support autoCommit=false"); + if (warningChain == null) warningChain = warning; + else warningChain.setNextWarning(warning); } }