Skip to content
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

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

Open
hareesh-kp opened this issue Jul 21, 2024 · 8 comments
Open
Labels
Backlog The topic in question has been recognized and added to development backlog Enhancement An enhancement to the driver. Lower priority than bugs.

Comments

@hareesh-kp
Copy link

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();
		} 
	}

}

@tkyc
Copy link
Member

tkyc commented Jul 22, 2024

Looking into it.. Have you tried any other driver versions where this is working? From reading the history on this, starting from issue 673, it looks like a regression.

@hareesh-kp
Copy link
Author

Issue can be replicated using driver version 12.6.1 jre11 with JDK 21. I haven't checked any other versions.

@tkyc tkyc added the Enhancement An enhancement to the driver. Lower priority than bugs. label Jul 23, 2024
@tkyc
Copy link
Member

tkyc commented Jul 23, 2024

Isn't a regression. Looks like this never worked at all. Added enhancement label for feature triage.

@Jeffery-Wasty Jeffery-Wasty added the Backlog The topic in question has been recognized and added to development backlog label Jul 24, 2024
@lilgreenbird lilgreenbird removed Enhancement An enhancement to the driver. Lower priority than bugs. Backlog The topic in question has been recognized and added to development backlog labels Aug 21, 2024
@lilgreenbird
Copy link
Contributor

lilgreenbird commented Aug 21, 2024

I don't think this is an enhancement, the error message clearly tells what the issue is. In your repro code, the error occurs only AFTER the resultset is closed? Please reference the object before closing. This is as designed. Please let us know if you have further questions otherwise we will be closing this issue.

@lilgreenbird lilgreenbird added the As designed The issue in question is as designed, and will not be addressed label Aug 21, 2024
@hareesh-kp
Copy link
Author

Please see issue #673 ( for CLOB ) which has been fixed years ago. It mentions #567 which is a similar issue with BLOB. As per the comment #567 (comment), it looks like the behavior doesn't reflect JDBC specification. Is SQLXML behavior expected to be different?

@lilgreenbird
Copy link
Contributor

hi @hareesh-kp

Thank you for the info. SQLXML datatype is not a type that's covered in the JDBC specs and the behavior you mentioned is not currently supported. I will add this as an enhancement request it will be considered along with other feature requests when we do planning for the next semester.

@lilgreenbird lilgreenbird removed the As designed The issue in question is as designed, and will not be addressed label Aug 26, 2024
@lilgreenbird lilgreenbird added Backlog The topic in question has been recognized and added to development backlog Enhancement An enhancement to the driver. Lower priority than bugs. labels Aug 26, 2024
@hareesh-kp
Copy link
Author

Thank you.

@harawata
Copy link
Contributor

FYI, JDBC 4.3 Specification - page 140

Note – The closing of a ResultSet object does not close the Blob, Clob, NClob or SQLXML objects created by the ResultSet. Blob, Clob, NClob and SQLXML objects remain valid for at least the duration of the transation [sic] in which they are created, unless their free method is invoked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backlog The topic in question has been recognized and added to development backlog Enhancement An enhancement to the driver. Lower priority than bugs.
Projects
Status: Backlog
Development

No branches or pull requests

5 participants