Cast exception when getting value of a sql_variant
See original GitHub issueDriver 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
- Expected behaviour:
resultset.getLong(colName)
to return numeric value as long - Actual behaviour:
java.lang.ClassCastException
:java.lang.Integer
cannot be cast tojava.lang.Long
- 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)
- 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 - usinggetLong(...)
forstart_value
etc. - BUT since
start_value
is asql_variant
and if particular sequence is ofint
data type class cast exception is thrown, when reading that value and Hibernate falls back to using safe defaults instead of actual capabilities (includingsupportsNamedParameters = 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 issql_variant
another logic overrides the expectedBIGINT
type with the type detected fromsql_variant
. So anInteger
is being returned up the stack where explicit casting is performed toLong
. 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
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:
- Created 3 years ago
- Comments:16 (8 by maintainers)
Top 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 >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
@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.
PR #1320 merged, closing issue.