[ 
https://issues.apache.org/jira/browse/DERBY-2472?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12497266
 ] 

Knut Anders Hatlen commented on DERBY-2472:
-------------------------------------------

The patch that replaced StandardException.setNestedException() with 
Throwable.initCause() may have introduced a problem in 
GenericLanguageConnectionContext.dropAllDeclaredGlobalTempTables() and 
BasicDependencyManager.coreInvalidateFor(). These two methods accumulate 
StandardExceptions which are thrown from within a loop, and the exceptions are 
re-thrown when the loop has ended. The exception that is re-thrown is the last 
exception that was caught, and it has all previously caught exceptions as its 
cause. The code that accumulates the exceptions looks like this:

        } catch (StandardException e) {
                e.initCause(topLevelStandardException);
                topLevelStandardException = e;
        }

The problem is that initCause() can only be called once on a Throwable, so if 
the StandardException already has a cause, initCause() will throw an 
IllegalStateException. The old code would not fail if this happened since 
setNestedException() would happily overwrite any existing nested exception.

One solution is to call initCause() on the exception's cause (or the cause of 
the cause, etc) if it has one. However, this wouldn't work if the last 
exception in the chain has been initialized with null as cause, since null also 
blocks subsequent calls to initCause(). Therefore, I suggest that we change the 
code so that it tries to chain the exceptions as suggested above, but if the 
StandardException cannot add a new exception to its chain, a new exception is 
created with the same SQL state, message and stack trace, but with no cause, 
and the clone is inserted into the chain instead.

> Use Throwable.initCause() to improve error reporting
> ----------------------------------------------------
>
>                 Key: DERBY-2472
>                 URL: https://issues.apache.org/jira/browse/DERBY-2472
>             Project: Derby
>          Issue Type: Improvement
>          Components: JDBC, Miscellaneous
>    Affects Versions: 10.3.0.0
>            Reporter: Kristian Waagan
>         Assigned To: Knut Anders Hatlen
>            Priority: Minor
>         Attachments: derby-2472_initial_patch.diff, embedsqlexception.diff, 
> embedsqlexception.stat, seenext.diff, seenext.stat, setStackTrace-v2.diff, 
> setStackTrace.diff, standardexception.diff, standardexception.stat
>
>
> As Derby has moved to J2SE 1.4, Throwable.initCause() can now be used to 
> improve error reporting.
> As stated in the initial commit (that was backed out, see below):
> 'Set Throwable.initCause() in a couple of locations at the JDBC level where 
> we setup nested SQLExceptions.
> This means that automatically the stack trace for a database failed to start 
> includes the reason
> for the failure, rather than just "see next exception". This is a great help 
> when running
> JUnit tests and getting failures to start a database.'
> The initial commit was backed out because it broke numerous tests under JDK 
> 1.6, and the author indicated he had no time to investigate.
> The patch caused no failures when running the tests with Java SE 5.0.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to