Skip to content

Commit af726cd

Browse files
srowengatorsmile
authored andcommitted
[SPARK-20459][SQL] JdbcUtils throws IllegalStateException: Cause already initialized after getting SQLException
## What changes were proposed in this pull request? Avoid failing to initCause on JDBC exception with cause initialized to null ## How was this patch tested? Existing tests Author: Sean Owen <sowen@cloudera.com> Closes #17800 from srowen/SPARK-20459.
1 parent 2b2dd08 commit af726cd

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

  • sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc

sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,17 @@ object JdbcUtils extends Logging {
652652
case e: SQLException =>
653653
val cause = e.getNextException
654654
if (cause != null && e.getCause != cause) {
655+
// If there is no cause already, set 'next exception' as cause. If cause is null,
656+
// it *may* be because no cause was set yet
655657
if (e.getCause == null) {
656-
e.initCause(cause)
658+
try {
659+
e.initCause(cause)
660+
} catch {
661+
// Or it may be null because the cause *was* explicitly initialized, to *null*,
662+
// in which case this fails. There is no other way to detect it.
663+
// addSuppressed in this case as well.
664+
case _: IllegalStateException => e.addSuppressed(cause)
665+
}
657666
} else {
658667
e.addSuppressed(cause)
659668
}

0 commit comments

Comments
 (0)