mssql-jdbc: Cast exception when getting value of a sql_variant
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
- Expected behaviour:
resultset.getLong(colName)to return numeric value as long - Actual behaviour:
java.lang.ClassCastException:java.lang.Integercannot 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
StoredProcedureQueryparameters were not bound by name, but instead indexes. - Currently Hibernate is using
select * from INFORMATION_SCHEMA.SEQUENCESfor obtaining sequence information - usinggetLong(...)forstart_valueetc. - BUT since
start_valueis asql_variantand if particular sequence is ofintdata 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_variantsupport. - normally a
BIGINTparameter is propagated down the call stack, but if the the actual column type issql_variantanother logic overrides the expectedBIGINTtype with the type detected fromsql_variant. So anIntegeris being returned up the stack where explicit casting is performed toLong. Which fails. sql_variantreading 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"));
}
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 16 (8 by maintainers)
@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.