Sobes.tech
Junior — Middle

What is the maximum number of operations allowed within a single transaction?

sobes.tech AI

Answer from AI

In Go (Golang), the maximum number of operations in a single transaction is not limited by the language itself or the standard library. Restrictions depend on the database and driver used. For example, in SQL transactions, you can perform many operations as long as resources are not exhausted or the database limit is not exceeded.

It is important to remember that overly long transactions can lead to locking and decreased performance. Therefore, it is recommended to keep transactions short and logically connected.

An example of using a transaction in Go with a database:

trx, err := db.Begin()
if err != nil {
    log.Fatal(err)
}

_, err = trx.Exec("INSERT INTO users(name) VALUES(?)", "Alice")
if err != nil {
    trx.Rollback()
    log.Fatal(err)
}

_, err = trx.Exec("UPDATE accounts SET balance = balance - ? WHERE user_id = ?", 100, 1)
if err != nil {
    trx.Rollback()
    log.Fatal(err)
}

err = trx.Commit()
if err != nil {
    log.Fatal(err)
}

Here, you can add any number of operations as long as they are logically connected and do not exceed the database limits.