Skip to content

Reading data from XML column is not possible after closing the ResultSet #2479

Open
@hareesh-kp

Description

@hareesh-kp

Driver version

12.7.1.jre11-preview

SQL Server version

Microsoft SQL Server 2019

Client Operating System

Windows 11 Pro x64

JAVA/JVM version

openjdk 17.0.12

Table schema

create table XmlTable (xmldata xml )
insert into XmlTable(xmldata) values ('<d><data>data 1</data></d>')

Problem description

While reading SQLXML after closing the ResultSet, the code throws error. It works if the ResultSet is not closed.

Expected behavior

Even after closing the ResultSet, it should be possible to read data. CLOB type works this way.

Actual behavior

Cannot get data and getting the error "This SQLXML object has been freed. It can no longer be accessed."

Error message/stack trace

com.microsoft.sqlserver.jdbc.SQLServerException: This SQLXML object has been freed. It can no longer be accessed.
	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:242)
	at com.microsoft.sqlserver.jdbc.SQLServerSQLXML.checkReadXML(SQLServerSQLXML.java:195)
	at com.microsoft.sqlserver.jdbc.SQLServerSQLXML.getBinaryStream(SQLServerSQLXML.java:219)

Any other details that can be helpful

The issue was originally noticed while using Spring SimpleJdbcCall. Then found this closed issue #673. It looks related.

Code to reproduce the error

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Statement;

public class App {
	
    private static final String CONNECTION_STRING = "jdbc:sqlserver://<>;";
    private static final String USERNAME  = "<>";
    private static final String PASSWORD = "<>";

	public static void main(String[] args) {
		xmlTest();
	}

	static void xmlTest() {
		String selectSql = "select xmldata from XmlTable";
		try (Connection connection = DriverManager.getConnection(CONNECTION_STRING, USERNAME, PASSWORD);
				Statement statement = connection.createStatement();
				ResultSet rs = statement.executeQuery(selectSql)) {
			rs.next();
			System.out.println("Reading data: ");
			SQLXML xml = rs.getSQLXML("xmldata");
			
			// this works
			// System.out.println(new String(xml.getBinaryStream().readAllBytes(), StandardCharsets.UTF_16LE));
			
			rs.close();

			// this throws error
			System.out.println(new String(xml.getBinaryStream().readAllBytes(), StandardCharsets.UTF_16LE));

		} catch (SQLException | IOException e) {
			e.printStackTrace();
		} 
	}

}

Metadata

Metadata

Assignees

No one assigned

    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

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions