{"record":{"id":"f61aad649ed035cc","repo":"gastownhall/beads","slug":"failed-to-begin-transaction-w-f61aad","errorCode":null,"errorMessage":"failed to begin transaction: %w","messagePattern":"failed to begin transaction: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/dolt/store.go","lineNumber":2251,"sourceCode":"// falls back to the remote's configured refspecs (ParseRefSpecs ->\n// GetRefSpecs), which are remote config rather than session state, and a\n// fetch writes only remote-tracking refs, never the working branch. Neither\n// depends on this fresh connection's default checkout.\nfunc (s *DoltStore) execWithLongTimeout(ctx context.Context, query string, args ...any) error {\n\tcfg, err := mysql.ParseDSN(s.connStr)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to parse DSN for long-timeout connection: %w\", err)\n\t}\n\tcfg.ReadTimeout = 5 * time.Minute\n\tdb, err := sql.Open(\"mysql\", cfg.FormatDSN())\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to open long-timeout connection: %w\", err)\n\t}\n\tdefer db.Close()\n\tdb.SetMaxOpenConns(1)\n\ttx, err := db.BeginTx(ctx, nil)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to begin transaction: %w\", err)\n\t}\n\tif _, err := tx.ExecContext(ctx, query, args...); err != nil {\n\t\t_ = tx.Rollback()\n\t\treturn err\n\t}\n\treturn tx.Commit()\n}\n\n// execWithLongTimeoutNoTx executes a long-running Dolt stored procedure without\n// an explicit transaction. Push operations do not need the pull/merge conflict\n// handling above, and DOLT_PUSH has diverged from direct `dolt push` behavior\n// when wrapped in a SQL transaction.\n//\n// Audited for be-b0am's fresh-connection branch hazard: safe. Every caller\n// passes s.branch explicitly as a CALL DOLT_PUSH(...) arg, so this fresh\n// connection's default checkout never matters.\nfunc (s *DoltStore) execWithLongTimeoutNoTx(ctx context.Context, query string, args ...any) error {\n\tdb, err := s.oneShotConn(5 * time.Minute)","sourceCodeStart":2233,"sourceCodeEnd":2269,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/dolt/store.go#L2233-L2269","documentation":"execWithLongTimeout begins a transaction on the dedicated long-timeout connection to run a long query (e.g. fetch/pull). This error wraps tx.BeginTx failure — the driver could not get a connection or start a transaction within the context deadline.","triggerScenarios":"Calling execWithLongTimeout when db.BeginTx(ctx, nil) fails — pool cannot establish a connection (server down, port wrong, auth rejected) or ctx already cancelled/timed out.","commonSituations":"Dolt server stopped or restarted; wrong port/host in config; credentials rejected; long-past context deadline from a parent operation.","solutions":["Check server reachability (bd dolt status, ping host:port)","Verify credentials and DSN host/port","Retry with a fresh context if the parent deadline expired","Check server logs for connection limits or auth errors"],"exampleFix":"// before\nctx := context.Background()\nerr := store.execWithLongTimeout(ctx, query) // hangs then fails confusingly\n// after\nctx, cancel := context.WithTimeout(context.Background(), 6*time.Minute)\ndefer cancel()\nerr := store.execWithLongTimeout(ctx, query)","handlingStrategy":"retry","validationCode":"if err := db.PingContext(ctx); err != nil {\n    return fmt.Errorf(\"cannot reach server before long query: %w\", err)\n}\nif ctx.Err() != nil { return ctx.Err() }","typeGuard":null,"tryCatchPattern":"err := store.execWithLongTimeout(ctx, q)\nif err != nil {\n    if errors.Is(err, context.DeadlineExceeded) || isConnRefused(err) {\n        // check server, retry with fresh long context\n    }\n    return err\n}","preventionTips":["Give long ops a context budget exceeding ReadTimeout","Monitor Dolt server liveness before big fetches","Verify auth and port in config","Handle server restarts with reconnect logic"],"tags":["database","transaction","connection","timeout"],"backgroundTag":"connection-refused","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}