-
Notifications
You must be signed in to change notification settings - Fork 438
Fixed TDS message severity handling #2741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
divang
wants to merge
12
commits into
main
Choose a base branch
from
dev/divang/handle_fatal_severity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d1d1450
Fixed TDS meesage severity handling
c6b9733
Fix TDS stream corruption in onEnvChange() for severity 25 rollback s…
b12ff35
Reverted OnInfo() method changes for Serverity level 25
824706b
Moved the Severity 25 error logic to OnDone() method
b6463bc
Clean the code
fac18ef
Clean up the code 2
6940e67
Added test case to simulate the Severity 25 fatal error
7f8d46d
Fixed SevereError message
53cd897
Fixed R_severeError message validation in Test class.
2ebe30e
Fixed OnDone method
9f2266d
Added setErrorSeverity as 20
3871be8
Fixed test cases which were failed due to onDone method is rasing Err…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/test/java/com/microsoft/sqlserver/jdbc/TDSTokenHandlerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.microsoft.sqlserver.jdbc; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
/** | ||
* Unit test for TDSTokenHandler onDone method, specifically testing | ||
* severity 25 (fatal) error handling and SQLServerError generation. | ||
*/ | ||
public class TDSTokenHandlerTest { | ||
|
||
@Mock | ||
private TDSReader mockTdsReader; | ||
|
||
@Mock | ||
private SQLServerConnection mockConnection; | ||
|
||
@Mock | ||
private IdleConnectionResiliency mockSessionRecovery; | ||
|
||
@Mock | ||
private TDSCommand mockCommand; | ||
|
||
@BeforeEach | ||
void setUp() throws SQLServerException { | ||
MockitoAnnotations.openMocks(this); | ||
|
||
// Setup basic mocks | ||
when(mockTdsReader.getConnection()).thenReturn(mockConnection); | ||
when(mockConnection.getSessionRecovery()).thenReturn(mockSessionRecovery); | ||
when(mockTdsReader.getCommand()).thenReturn(mockCommand); | ||
|
||
// Mock the TDS data reading methods that StreamDone.setFromTDS() needs | ||
when(mockTdsReader.readUnsignedByte()).thenReturn(TDS.TDS_DONE); // token type | ||
when(mockTdsReader.readShort()).thenReturn((short) 0); // status and curCmd | ||
when(mockTdsReader.readLong()).thenReturn(0L); // rowCount | ||
} | ||
|
||
/** | ||
* Simplified test case specifically for Severity 25 fatal error simulation. | ||
* This test mocks the TDS status flags to simulate a severity 25 error | ||
* and verifies that SQLServerError is properly generated. | ||
*/ | ||
@Test | ||
void testSeverity25FatalErrorSimulation() throws SQLServerException { | ||
// Arrange - Simulate severity 25 fatal error with DONE_ERROR status | ||
short severity25ErrorStatus = (short) (TDS.DONE_ERROR); // 0x0002 - indicates error condition | ||
|
||
// Mock TDSReader to return the error status that would be set for severity 25 errors | ||
when(mockTdsReader.peekStatusFlag()).thenReturn(severity25ErrorStatus); | ||
|
||
// Override the readShort to return the error status for the StreamDone.setFromTDS() call | ||
// The first readShort call is for the status field in StreamDone | ||
when(mockTdsReader.readShort()) | ||
.thenReturn(severity25ErrorStatus); | ||
|
||
// Create token handler to test | ||
TDSTokenHandler handler = new TDSTokenHandler("Severity25Test"); | ||
|
||
// Act - Call onDone method which should detect the error status and create SQLServerError | ||
handler.onDone(mockTdsReader); | ||
|
||
// Verify that SQLServerError was generated due to the error status | ||
SQLServerError generatedError = handler.getDatabaseError(); | ||
assertNotNull(generatedError, "SQLServerError must be generated for severity 25 error condition"); | ||
|
||
// Verify the error message is the expected server error message | ||
String expectedErrorMessage = SQLServerException.getErrString("R_severeError"); | ||
assertEquals(expectedErrorMessage, generatedError.getErrorMessage(), | ||
"Generated error should have R_severeError message for fatal errors"); | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SqlClient treats DONE_SRVERROR as a fatal error:
https://github.com/dotnet/SqlClient/blob/v6.1.1/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs#L3167
Fatal errors ultimately close the connection and signal up to connection pools about the error. We need to plumb through something similar. See makeFromDatabaseError in SQLServerException.java. We don't have the database error, so we will need to figure something out there.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, adding Error Severity 20 will make sure the notifyPooledConnection() call.
SQLServerError syntheticError = new SQLServerError();
syntheticError.setErrorMessage(SQLServerException.getErrString("R_severeError"));
syntheticError.setErrorSeverity((byte) 20); // A severity of 20 indicates a fatal error in the current
// process.
Ref: makeFromDatabaseError in SQLServerException.java:
// Close the connection if we get a severity 20 or higher error class (nClass is severity of error).
if ((sqlServerError.getErrorSeverity() >= 20) && (null != con)) {
con.notifyPooledConnection(theException);
con.close();
}