I was wondering if you start a transaction with a context if the whole transaction will "listen" to that context here?
tx, _ := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable})
stmt, _ := tx.Prepare("SELECT id, timeout, lang FROM client WHERE id=?")
Or do you explicitly apply the context to each query?
tx, _ := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable})
stmt, _ := tx.PrepareContext(ctx, "SELECT id, timeout, lang FROM client WHERE id=?")
>Solution :
No. Prepare, and other contextless methods, use context.Background.
From the Tx.Prepare docs…
Prepare uses context.Background internally; to specify the context, use PrepareContext.
Looking at the source code, it’s just a simple wrapper.
func (tx *Tx) Prepare(query string) (*Stmt, error) {
return tx.PrepareContext(context.Background(), query)
}
I assume this is because you’re not supposed to store Contexts in structs.