As you state, the driver (and db) need to support this. The synchronization for something like this is internal. 

But, if the operations are modifying the same tables you may end up with serialized operations anyway in order to ensure consistency while avoiding deadlocks. 

On Apr 20, 2024, at 12:11 AM, nilsocket <nilsoc...@gmail.com> wrote:

Does any solution exist for this problem?

I'm facing similar problem, want to run bunch of queries inside a transaction,
but want to run them concurrently, as running sequential is taking more than 1 second.

On Friday, June 29, 2018 at 6:32:40 PM UTC+5:30 Space A. wrote:
Hi,

DB in common case will do lock/unlock when necessary for you implicitly. It is safe to call queries simultaneously. Moreover it is general approach in Enterprise solutions to have let's say hundreds of open connections sockets towards DB and execute statements from hundreds of threads. However to answer your question accurate you need to look at underlying implementation. As I understand, most of Go DB implementations don't even have connection pool capabilities. So they are even "safer" from that point because single network socket will become additional (and unnecessary) "serialization" mechanism. You could probably try to implement connection pool by yourself.


Regards,


среда, 27 июня 2018 г., 15:19:27 UTC+3 пользователь Ain написал:
Hi

It is my understanding that DB Tx is generally not safe to use concurrently, ie consider following code:

    tx, err := db.Begin()
    defer tx.Rollback()
    ...
    statements[0], err := tx.Prepare("...")
    defer s1.Close()
    statements[1], err := tx.Prepare("...")
    defer s2.Close()
    ...
    wg.Add(len(statements))
    for x := range statements {
        go func(x int) {
            defer wg.Done()
            statements[x].QueryRow(...).Scan(...)
        }(x)
    }
    wg.Wait()

Here we prepare bunch of statements for a Tx which are "independent of each other", ie query different tables and scan to different variables etc. However they can't be executed concurrently, in a goroutine, as they share the same connection and thus interfere with each other?

It might be safe when db driver takes care to serialise the statements but database/sql doesn't guarantee safe usage for given secenario.
Is that correct?

So how would you go about executing multiple queries in the same transaction context concurrenty?


TIA
ain

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/4c798067-55cd-4d20-9eba-875c4743ce73n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/B34F5737-742D-45F2-8917-8C60EC79BF6D%40ix.netcom.com.

Reply via email to