I’m using derby 10.10.1.1 on windows and am encountering an issue when shutting down derby. After tracking down the problem, there seems to be a possible bug in the DRDAConnThread.java – before filing a jira item, I wanted to run this by
the community for feedback.
The issue manifests itself when shutting down derby – under certain circumstances, a NullPointerException is thrown. Even worse – the exception is “hidden” since the handling of the exception itself throws an exception. This issue is not
consistent in our tests and presents unpredictably – most likely a timing issue. While I don’t have a firm grasp of the exact circumstances that cause the exception to be thrown, I believe it is clear that the handling of an exception is less than ideal and
leads to this exception. Tracing through the code in DRDAConnThread.java will provide the best explanation:
Code enters parseSECCHK (line 3117) to perform security check on a new session
All security checks pass and control continues to line 3357 to the call
That method call generates an exception as derby is shutting down as follows:
java.sql.SQLNonTransientConnectionException: No current connection.
Caused by: java.sql.SQLException: No current connection.
... 10 more
This gets caught in line 3365 and calls handleException
handleException attempts to inform the client of the exception and then calls closeSession and close on lines 8759 and 8760.
closeSession() sets the session member variable to null.
This is the crux of the issue
After handleException is called, control returns to parseSECCHK() and continues to line 3374 (as all security checks passed).
This line attempts to deference a member of session which has just been set to null by closeSession (in step 6) !!
This will throw a NullPointerException which is passed up the stack and eventually caught and handled on line 320
This handling calls handleException(e)
handleException then calls sendUnexpectedException which attempts to dereference
session once again on line 8794, which of course throws a NullPointerException.
This NPE is never caught, thus handled by the uncaughtExceptionHandler which by default terminates the thread (note that this causes the above exception to step 3 to be “hidden” as only this exception is bubbled to the uncaughtExceptionHandler)
However, we have set a default uncaughtExceptionHandler in our application which terminates on an uncaught exception. This is undesirable in this situation since we are only trying to terminate an embedded derby instance, but do not
wish our application to terminate. However, this default behavior makes sense as an uncaught exception usually indicates a serious error.
Thus, it seems that the handling of an exception thrown by resetFromPool() needs work. I’m not sure what the “correct” behavior should be but throwing a DRDAProtocolException.newDisconnectException after handleException does “fix” the issue.
Shall I file a bug? Is there anything else I should be aware of?