question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cast exception when getting value of a sql_variant

See original GitHub issue

Driver version

  • 7.4.1
  • 8.2.1

SQL Server version

12.0.2000.8

Client Operating System

Win10

JAVA/JVM version

Oracle’s 1.8.0

Table schema

INFORMATION_SCHEMA

Problem description

  1. Expected behaviour: resultset.getLong(colName) to return numeric value as long
  2. Actual behaviour: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
  3. Error message/stack trace:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
	at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getLong(SQLServerResultSet.java:2340)
  1. Any other details that can be helpful:
  • Detected while investigating problem why StoredProcedureQuery parameters were not bound by name, but instead indexes.
  • Currently Hibernate is using select * from INFORMATION_SCHEMA.SEQUENCES for obtaining sequence information - using getLong(...) for start_value etc.
  • BUT since start_value is a sql_variant and if particular sequence is of int data type class cast exception is thrown, when reading that value and Hibernate falls back to using safe defaults instead of actual capabilities (including supportsNamedParameters = false)
  • Quick workaround in the project was to correct the datatype of two sequences. But clearly there is a problem in the driver with sql_variant support.
  • normally a BIGINT parameter is propagated down the call stack, but if the the actual column type is sql_variant another logic overrides the expected BIGINT type with the type detected from sql_variant. So an Integer is being returned up the stack where explicit casting is performed to Long. Which fails.
  • sql_variant reading logic should consider the data type provided by the caller to avoid class cast exception. Normally it is expected that a wider data type should contain the narrow data type implicitly.

JDBC trace logs

jdbcTrace.log

Reproduction code

ResultSet rs = conn
                .prepareStatement("select cast(1 as sql_variant) as [sad_face]")
                .executeQuery();
if (rs.next()) {
     println ("Long value: " + rs.getLong("sad_face"));
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
zoladkowcommented, Apr 28, 2020

@peterbae Ok, you’ve beat me with your last answer.

Thank you. Now I’m reassured this case is well understood and will be addressed properly. With benefit of having JDBC portability maintained - the driver covering the SqlServer specifics as much as possible.

0reactions
peterbaecommented, May 12, 2020

PR #1320 merged, closing issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql_variant read using Entity Framework as an object cannot ...
Attempting to cast to a double gives me an error about how I can't cast an object to a double, MinWeightVariant.GetType() just gives...
Read more >
Problems Caused by Use of the SQL_VARIANT Datatype
A sql_variant cannot be passed directly to some SQL operators and functions such as LIKE , SUM() or AVG() , and can give...
Read more >
Using Sql_variant data type - JDBC Driver for SQL Server
Learn about the sql_variant data type in the JDBC driver and how it can be used to support table-valued parameters (TVP) and bulk...
Read more >
Convert varchar value to int without throwing an exception in ...
[Solved]-Convert varchar value to int without throwing an exception in case of bad ... TRY_CAST returns the value as SQL_VARIANT type; if the...
Read more >
Overview of SQL Server sql_variant Data Type
The SQL_VARIANT_PROPERTY Function · Expression: it contains the value you want to get info on · Property: it is the name of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found