2020-04-09 11:15:34 +00:00
|
|
|
package tx
|
2020-04-08 19:20:23 +00:00
|
|
|
|
2020-04-09 11:15:34 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Begin(ctx context.Context, db *sql.DB) {
|
|
|
|
tx, err := db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
if _, err = tx.ExecContext(ctx, `UPDATE users SET name = "Tyador Borlú" WHERE id = 1`); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-08 19:20:23 +00:00
|
|
|
|
2020-04-09 11:15:34 +00:00
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-08 19:20:23 +00:00
|
|
|
}
|