Home >Backend Development >Golang >How to use transactions in Golang?
In Go, you can use the Tx type for transaction operations. To start a transaction, use db.Begin(). In the transaction block, database operations such as queries and updates are performed. After successful execution, use tx.Commit() to commit the transaction. In practice, transactions ensure consistency for concurrent operations, such as updating inventory and creating orders simultaneously.
In Go, a transaction is a collection of atomic operations that either all succeed or all fail. They are used to ensure the integrity of database operations, especially in situations where multiple concurrent operations could cause data corruption.
Transactions in Go are represented by the Tx
type in database/sql
. To start a transaction, do the following:
tx, err := db.Begin() if err != nil { return fmt.Errorf("failed to begin transaction: %w", err) }
In the transaction block, you can perform any database operation. For example, the following code selects a record from a table and updates it with a new value:
var id, version int if err := tx.QueryRow("SELECT id, version FROM example WHERE name = ?", "foo").Scan(&id, &version); err != nil { return fmt.Errorf("failed to select: %w", err) } result, err := tx.Exec("UPDATE example SET version = ? WHERE id = ? AND version = ?", version+1, id, version) if err != nil { return fmt.Errorf("failed to update: %w", err) }
After all operations have been successfully performed, you can use tx.Commit()
to commit the transaction:
if err := tx.Commit(); err != nil { // 放弃事务 if err := tx.Rollback(); err != nil { return fmt.Errorf("failed to rollback transaction: %w", err) } return fmt.Errorf("failed to commit transaction: %w", err) }
Consider an online store database, which includes a products
table and an orders
table. We want to create a transaction to do the following:
func CreateOrder(db *sql.DB, productID int, quantity int) error { tx, err := db.Begin() if err != nil { return fmt.Errorf("failed to begin transaction: %w", err) } var stock int if err := tx.QueryRow("SELECT stock FROM products WHERE id = ?", productID).Scan(&stock); err != nil { return fmt.Errorf("failed to select: %w", err) } if stock < quantity { return tx.Rollback() // 放弃事务,因为没有足够的库存 } if _, err := tx.Exec("UPDATE products SET stock = ? WHERE id = ?", stock-quantity, productID); err != nil { return fmt.Errorf("failed to update: %w", err) } if _, err := tx.Exec("INSERT INTO orders (product_id, quantity) VALUES (?, ?)", productID, quantity); err != nil { return fmt.Errorf("failed to insert: %w", err) } if err := tx.Commit(); err != nil { return fmt.Errorf("failed to commit transaction: %w", err) } return nil }
By using transactions, we ensure that the store's inventory is always accurate, even if there are multiple concurrent orders trying to subtract inventory from the same product.
The above is the detailed content of How to use transactions in Golang?. For more information, please follow other related articles on the PHP Chinese website!