microsoft / mssql-jdbc

The Microsoft JDBC Driver for SQL Server is a Type 4 JDBC driver that provides database connectivity with SQL Server through the standard JDBC application program interfaces (APIs).
MIT License
1.06k stars 427 forks source link

Batch insert behaviour with/without auto commmit? #2526

Closed JDBC-0 closed 19 hours ago

JDBC-0 commented 1 month ago

Question

How does the driver execute batch INSERTs if the connection auto commit is true? The JDBC specification has no standard rule and this "is a driver specific" topic. The documentation https://learn.microsoft.com/en-us/sql/connect/jdbc/performing-batch-operations?view=sql-server-ver16 has no hint for this - so how can we be sure that our batch inserts are performed as expected (as batches - and not as single INSERTs) - if auto commit is true?

divang commented 1 month ago

When autocommit is true:

When autocommit is false:

Summary: True: Immediate commits for each statement. False: All statements in the batch are treated as one transaction, allowing for rollbacks on failure.

JDBC-0 commented 3 weeks ago

@divang : thanks - but I guess this is not always true, is it? Because it might depend on the URL Parameters like the bulkCopy feature. I guess even if autocommit is true, the bulk copy feature won't commit each single INSERT statement ... right? Does the bulk copy feature always commits all inserted rows at once with 1 single transaction? If not, the bulk copy feature wouldn't have any advantage if every single INSERT statement would be commited. Or am I missing a point?

machavan commented 3 weeks ago

Batch inserts done using prepared statements with URL parameter useBulkCopyForBatchInsert=true also adhere to transactional semantics. The inserted records are committed upon committing the transaction.

The behavior mentioned above by @divang is consistent with useBulkCopyForBatchInsert=true also

Jeffery-Wasty commented 6 days ago

Hi @JDBC-0,

Please let us know if you have additional questions/comments, or else we will move forward with closing this issue.

JDBC-0 commented 3 days ago

Thank you for the clarification. To avoid the same questions again, it would be very helpful to have this info in the driver documentation.

Jeffery-Wasty commented 19 hours ago

We'll look into updating docs for the next release.