Skip to content

Bulk copy for batch insert destroys data without error #2669

Open
@literakl

Description

@literakl

Driver version

12.10.0.jre11

SQL Server version

16

Client Operating System

Windows 11

JAVA/JVM version

Java 18

Table schema

create table states (
[Id] identity not null
[StateCode] nvarchar NOT NULL
)

Problem description

var items = List.of(new State("IL"));
final var session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try (PreparedStatement ps = connection.prepareStatement("insert into states (StateCode) values (?)")) {
for (T entity : items) {
ps.setString(1, entity.getState());
ps.addBatch();
}
ps.executeBatch();
}

Expected behavior

Select StateCode from states;
OH

Actual behavior

Select StateCode from states;
"[B@4b01d9e4

Error message/stack trace

There is no error anywhere. I debugged the MS SQL JDBC driver up to com.microsoft.sqlserver.jdbc.AppDTVImpl#setValue.

When I turned off dataSource.setUseBulkCopyForBatchInsert(true);
then it started to work properly.

I found https://learn.microsoft.com/en-us/sql/connect/jdbc/use-bulk-copy-api-batch-insert-operation?view=sql-server-ver16. which states that identity columns are not supported. So that the array of bytes of the strings are persisted as an object reference instead of the array content, may be the reason, why it's not supported.

If this is the case and cannot be fixed, then please raise an exception when an identity column is detected. This took me hours to identify.

https://stackoverflow.com/questions/79632176/ms-sql-jdbc-driver-saves-string-as-byte-array-object-reference

Metadata

Metadata

Assignees

Labels

EnhancementAn enhancement to the driver. Lower priority than bugs.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions