From ad3853336331102ff9e76ef4be09908a1e643a65 Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Fri, 28 Apr 2017 09:13:01 +0100 Subject: [PATCH 1/2] Avoid failing to initCause on JDBC exception with cause initialized to null --- .../spark/sql/execution/datasources/jdbc/JdbcUtils.scala | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala index 5fc3c2753b6cf..ac1f49d6c4d3e 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala @@ -653,7 +653,13 @@ object JdbcUtils extends Logging { val cause = e.getNextException if (cause != null && e.getCause != cause) { if (e.getCause == null) { - e.initCause(cause) + try { + e.initCause(cause) + } catch { + // cause may have been explicitly initialized to null, in which case this + // fails. No way to detect it, can only catch the exception. + case _: IllegalStateException => e.addSuppressed(cause) + } } else { e.addSuppressed(cause) } From f6f2b7a16ce7cc8870d05732b51ef796906db937 Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Mon, 1 May 2017 19:22:53 +0100 Subject: [PATCH 2/2] Clarify comments --- .../spark/sql/execution/datasources/jdbc/JdbcUtils.scala | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala index ac1f49d6c4d3e..0183805d56257 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala @@ -652,12 +652,15 @@ object JdbcUtils extends Logging { case e: SQLException => val cause = e.getNextException if (cause != null && e.getCause != cause) { + // If there is no cause already, set 'next exception' as cause. If cause is null, + // it *may* be because no cause was set yet if (e.getCause == null) { try { e.initCause(cause) } catch { - // cause may have been explicitly initialized to null, in which case this - // fails. No way to detect it, can only catch the exception. + // Or it may be null because the cause *was* explicitly initialized, to *null*, + // in which case this fails. There is no other way to detect it. + // addSuppressed in this case as well. case _: IllegalStateException => e.addSuppressed(cause) } } else {