shad-go/lectures/07-sql/resources/tx.go

36 lines
658 B
Go
Raw Normal View History

2020-04-09 13:00:41 +00:00
package resources
import (
"context"
"database/sql"
"log"
)
func TxExhaust(ctx context.Context, db *sql.DB) {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
log.Println(err)
return
}
if _, err = tx.ExecContext(ctx, `UPDATE users SET name = "Surl/Tesh-echer" WHERE id = 1`); err != nil {
log.Println(err)
return
}
if err = tx.Commit(); err != nil {
log.Println(err)
}
}
func TxDeadlock(ctx context.Context, db *sql.DB) {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
log.Fatal(err)
}
defer tx.Rollback()
_, _ = db.QueryContext(ctx, "SELECT id, name FROM users")
_, _ = db.QueryContext(ctx, "SELECT id, name FROM users")
}