Exception using setMaxRows() followed by query execution when showplan_text is on
See original GitHub issueDriver version or jar name
6.0.8112.100
SQL Server version
14.0 (Linux)
Client operating system
macOS
Java/JVM version
1.8 OpenJDK
Problem description
When the SHOWPLAN_TEXT is on, driver may fail on statement execution. Especially, using setMaxRows() on statement before execution. The problem is that before querying driver sends SET ROWCOUNT to the database and expects nothing as a results. But showplan_text is on and database will return result set causing driver failure with:
com.microsoft.sqlserver.jdbc.SQLServerException: The TDS protocol stream is not valid. Unexpected token TDS_COLMETADATA (0x81).
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2400)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2384)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.throwInvalidTDSToken(SQLServerConnection.java:2373)
at com.microsoft.sqlserver.jdbc.TDSReader.throwInvalidTDSToken(IOBuffer.java:6558)
at com.microsoft.sqlserver.jdbc.TDSParser.throwUnexpectedTokenException(tdsparser.java:119)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onColMetaData(tdsparser.java:247)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:87)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:36)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$1ConnectionCommand.doExecute(SQLServerConnection.java:2495)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectionCommand(SQLServerConnection.java:2500)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.setMaxRows(SQLServerConnection.java:519)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.setMaxRowsAndMaxFieldSize(SQLServerStatement.java:810)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:848)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:778)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:166)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery(SQLServerStatement.java:677)
Expected behavior and actual behavior
I think the code should be executed without errors.
Actually there is an exception.
Repro code
Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=testdb", "test", "***");
DatabaseMetaData data = connection.getMetaData();
connection.createStatement().execute("SET SHOWPLAN_TEXT ON");
Statement statement = connection.createStatement();
statement.setMaxRows(20);
statement.executeQuery("SELECT 1");
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:13 (4 by maintainers)
Top Results From Across the Web
SET SHOWPLAN_TEXT (Transact-SQL) - Microsoft Learn
When SET SHOWPLAN_TEXT is OFF, SQL Server executes statements without generating a report with execution plan information.
Read more >sql server - An exception occurred while executing a transact ...
I got this error message while trying to attach DB, if it's your case, go to to the folder where your DB is:....
Read more >SQL Server query execution plans – Viewing the plans
In this article, we will describe the methods for opening the SQL Server query execution plans, both actual and estimated ones.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @tseylerd ,
Looks like I accidentally uploaded the wrong jars. Please try the updated zip file again, I apologize for inconvenience.
I added a few more lines that generate additional log for this case. I agree that removing the exception might not seem like the best approach, but other solutions I looked into either result in performance degradation or do not completely solve the issue.
Please also feel free to create and review pull requests, we appreciate contributions from the community.
Closing the issue since PR merged and fix released with 6.5.1 preview release.