Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch insert behaviour with/without auto commmit? #2526

Open
JDBC-0 opened this issue Oct 7, 2024 · 3 comments
Open

Batch insert behaviour with/without auto commmit? #2526

JDBC-0 opened this issue Oct 7, 2024 · 3 comments
Assignees

Comments

@JDBC-0
Copy link

JDBC-0 commented Oct 7, 2024

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
Copy link

divang commented Oct 14, 2024

When autocommit is true:

  • Each statement in the batch is treated as an individual transaction.
  • As soon as executeBatch() is called, each SQL statement is automatically committed after execution.
  • If one statement fails, it won't affect the already executed statements since they are committed immediately.

When autocommit is false:

  • The batch is treated as a single transaction.
  • None of the statements will be committed until you explicitly call commit().
  • If one statement fails, you can roll back the entire transaction, which means none of the statements in the batch will take effect.

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
Copy link
Author

JDBC-0 commented Nov 4, 2024

@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
Copy link

machavan commented Nov 7, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: To be triaged
Development

No branches or pull requests

3 participants