Skip to content

When running sql in selectMethod=cursor, SET STATISTICS_XML_ON crashes TDSParser #2665

@siggemannen

Description

@siggemannen

Driver version

12.8.1.jre8

SQL Server version

Microsoft SQL Server 2022 (RTM) - 16.0.1000.6 (X64)

Client Operating System

Windows 10

JAVA/JVM version

1.8

Problem description

When using connection string with: selectMethod=cursor, and running something like

set statistics xml on
select 1

driver throws exception in TDSReader#throwInvalidTDSToken

This is probably because unlike TDSTokenHandler#onColMetaData, TDSTokenHandler#onRow doesn't properly handle extra metadata/resultset coming from SHOWPLAN.

Expected behavior

I expect to be able to run the above query without any issues.

Actual behavior

Below error message. Interesting enough, the result is returned back to the client and can be parsed, but then the connection is terminated.

Error message/stack trace

May 13, 2025 10:52:22 PM com.microsoft.sqlserver.jdbc.TDSReader throwInvalidTDSToken
SEVERE: ConnectionID:41 ClientConnectionId: 5b22c202-f6c6-40b2-a3a6-81e22d6ff14d got unexpected value in TDS response at offset:153

Any other details that can be helpful

A complete example to recreate the issue:

public class CursorExecTest
{
    @Test
    public void test_cursor() throws Exception
    {
        try (Connection c = DriverManager.getConnection(
                "jdbc:sqlserver://localhost\\sql2022;user=user;password=pass;lastUpdateCount=false;encrypt=false;trustServerCertificate=true;selectMethod=cursor;responseBuffering=adaptive;");
                Statement s = c.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);)
        {
            s.execute("SET STATISTICS XML ON;");
            boolean exec = s.execute("SELECT 1");
            if (exec)
            {
                ResultSet rs = s.getResultSet();
                while (rs.next())
                {
                    System.out.println(rs.getInt(1));
                }
            }
        }

    }
}

Metadata

Metadata

Assignees

Labels

BacklogThe topic in question has been recognized and added to development backlogEnhancementAn enhancement to the driver. Lower priority than bugs.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions