question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add function to allow batch inserts without transaction

See original GitHub issue

Hello.

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:open
  • Created 8 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kaeedocommented, Feb 9, 2023

@Zaid-Ajaj Sorry, maybe i should’ve explained my use case first. At my job, we’re making use of executeTransactionAsync to 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 executeTransactionAsync because 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 to use 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

0reactions
pihaicommented, Jun 16, 2023

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 executeTransactionAsync to 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found