Add function to allow batch inserts without transaction
See original GitHub issueHello.
Currently, to do a batch insert you would use the Sql.executeTransactionAsync with a list of queries and parameters. However, this function starts and commits a transaction implicitly. Would it be possible to get a function that doesn’t implicitly create a transaction, allowing for separate control similar to this section in the readme. So essentially manually starting a transaction, then executing a list of queries with a single trip to the DB, and then manually committing/rolling back the transaction.
Thanks
Issue Analytics
- State:
- Created 8 months ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Transaction vs Batch Query to Avoid Duplicate MySQL Inserts
You can avoid duplicates by wrapping the code in a transaction (batch insert isn't required to stop duplicates). It's better to use 1...
Read more >MySQL: Why is inserting data with transactions faster then ...
1 Answer 1 ... Without explicit transaction, all statements are in auto-commit mode - so each insert is separate transaction. And that comes...
Read more >Spring JDBC Batch Inserts
In this tutorial, we'll learn how to effectively insert a vast amount of data into our target RDBMS using Spring JDBC Batch support, ......
Read more >Batch Insert/Update with Hibernate/JPA
In this tutorial, we'll learn how we can batch insert and update entities using Hibernate/JPA. Batching allows us to send a group of...
Read more >batch insert question- One Transaction?
I've built up an SQL batch insert statement within a string variable, and I'm passing it to the DB Query mechanism with no...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@Zaid-Ajaj Sorry, maybe i should’ve explained my use case first. At my job, we’re making use of
executeTransactionAsyncto insert many items into the DB. However, now we have the need to manually either commit or rollback the transaction based on some other things happening shortly before or after the call to insert the items.We can manually start a transaction, but then we can’t call
executeTransactionAsyncbecause it also starts a transaction internally, and postgres doesn’t support nested transactions (only savepoints)What I propose is a new function
executeManyAsync(or similar name) that takes a transaction as a parameter and hands that touse batch = new NpgsqlBatch(connection, transaction)instead of creating a transaction internally.I’m willing to contribute a pull request if you’re happy with this addition
I agree with @kaeedo that the ability to execute commands in batch without implicitly opening a transaction would be a great addition.
We use the Unit of work pattern to manage our transactions. The repositories use Npgsql.FSharp to execute SQL commands. Currently we cannot use
executeTransactionAsyncto run multiple commands in batch because there is already a transaction open in the unit of work.Even though our use case is to insert multiple rows in a batch, such API could also be used to execute multiple queries and read the results back. See the example here: https://www.npgsql.org/doc/performance.html#batchingpipelining