mssql-jdbc: Float / double type values are sent to MSSQL in an incompatible format
Driver version
8.2.2.jre8
SQL Server version
Microsoft SQL Server 2019 (RTM-CU28) (KB5039747) - 15.0.4385.2 (X64) Jul 25 2024 21:32:40 Copyright © 2019 Microsoft Corporation Developer Edition (64-bit) on Linux (Ubuntu 20.04.6 LTS) <X64>
(mcr.microsoft.com/mssql/server:2019-latest docker image)
Client Operating System
Ubuntu in WSL (same as DB docker host)
> lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.4 LTS
Release: 22.04
Codename: jammy
> uname -a
Linux 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
Docker Host: Microsoft Windows 11 Pro version 10.0.22631 Build 22631
JAVA/JVM version
java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
Table schema
Example:
create table t (
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Flag] [bit] NOT NULL,
[FKID] [int] NULL,
[RecordInsert] [datetime2](3) NOT NULL,
[TimeStamp] [datetime2](3) NOT NULL,
[Col1] [decimal](9, 2) NULL,
[Col2] [decimal](9, 2) NULL,
...
)
In reality, 2 tables are used, with > 1000 columns each, of different data types. These tables are used to hold time-series telemetry data. The data is inserted with a stored procedure, which handles splitting up the data between the 2 tables.
Problem description
When using a prepared statement, passing double values (using setObject w/o a specified target type) to a batch insert causes some double values to be sent ~as the exact Base-10 representation~ with a precision of 43 digits, instead of a valid MSSQL float type. This causes the error: “Error converting data type float to decimal”.
Expected behavior
Double and float values should be sent to SQL server ~in a way that ensures they can be processed by SQL server~ with a precision of 38 digits. According to Conversions on setObject in the documentation, my Java Double type values should be passed as “DOUBLE (float)” values. I would then expect SQL server to perform the required conversion to the relevant Decimal type.
Actual behavior
Using the SQL server Profiler, I can see the following event on CallableStatement.executeBatch:
exec sp_executesql N'EXEC MyInsertProcedure @P0,@P1,@P2,@P3,@P4,@P5,@P6,... ,
N'@P0 datetime2,@P1 int,@P2 int,@P3 int,@P4 int,@P5 float,@P6 float,,... ,
'2024-09-17 17:06:12.2400000',217,37,11,30,5.050000000000000710542735760100185871124268,0.2000000000000000111022302462515654042363167,...
Note the floating point values have far too much precision (even for a Java double afaik). The expected values in this case are 5.05 and 0.2. I think this is the source of the issue.
This event is followed by similar calls for the rest of the batch, though seemingly with an “indirection” for the stored procedure.
Error message/stack trace
java.sql.BatchUpdateException: Error converting data type float to decimal.
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeBatch(SQLServerPreparedStatement.java:2075)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy33.executeBatch(Unknown Source)
Any other details that can be helpful
I encountered this issue while debugging some legacy codebase. Interestingly, this issue never showed up in production, which uses the following SQL Server version:
Microsoft SQL Server 2019 (RTM-CU15) (KB5008996) - 15.0.4198.2 (X64) Jan 12 2022 22:30:08 Copyright © 2019 Microsoft Corporation Standard Edition (64-bit) on Windows Server 2016 Datacenter 10.0 <X64> (Build 14393: ) (Hypervisor)
I think a solution to this problem (as well as a far more efficient approach) is to explicitly pass the storage type to setObject.
In other words, I likely have a solution / workaround to the problem, but I would like to understand this issue better. My current efforts are ensuring compatibility of newer versions of the software.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (7 by maintainers)
Thank you for your replies, I’ll look into this further and get back to you with ideas and next steps.