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
|
|
|
|
}
|
|
|
|
|
2021-04-08 11:37:21 +00:00
|
|
|
_, err = tx.ExecContext(ctx, `UPDATE users SET name = "Surl/Tesh-echer" WHERE id = 1`)
|
|
|
|
if err != nil {
|
2020-04-09 13:00:41 +00:00
|
|
|
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")
|
|
|
|
}
|