package main
import (
"context"
"database/sql"
"fmt"
_ "github.com/lib/pq"
"time"
)
func main() {
const delay_in_secs = 10
db, err := sql.Open("postgres", "postgres://localhost/postgres?sslmode=disable")
if err != nil {
panic(fmt.Sprintf("failed to open DB: %v", err))
}
ctx, cancelFunc := context.WithTimeout(context.Background(), (delay_in_secs/2)*time.Second)
defer cancelFunc()
start := time.Now()
defer func() {
fmt.Printf("Total time: %v\n", time.Since(start))
}()
tx, err := db.BeginTx(ctx, nil)
if err != nil {
panic(fmt.Sprintf("failed to begin TX: %v", err))
}
defer tx.Commit()
_, err = tx.ExecContext(ctx, fmt.Sprintf("SELECT pg_sleep(%d", delay_in_secs))
if err != nil {
panic(fmt.Sprintf("failed to begin TX: %v", err))
}
}
I noticed if I have a syntax error like this example with a missing paren the query exec will not return with an error but will block because of the context being used. So this hangs forever.
I noticed if I have a syntax error like this example with a missing paren the query exec will not return with an error but will block because of the context being used. So this hangs forever.
version: go1.13