Transactions
Commit and roll back.
Why Transactions
A transaction groups several statements so they all succeed or all fail together. This keeps data consistent, for example moving money between two accounts without losing it midway.
- Atomic: all or nothing
- Consistent state
- Isolated from other transactions
db.Begin
Begin starts a transaction and returns a *sql.Tx. You run statements on the Tx, not on the pool, so they share one connection and one transaction.
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}