{"record":{"id":"1f13b4ff2fd3e48e","repo":"apache/beam","slug":"failed-to-open-database-v","errorCode":null,"errorMessage":"failed to open database: %v","messagePattern":"failed to open database: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/io/databaseio/database.go","lineNumber":75,"sourceCode":"\treturn beam.ParDo(s, &queryFn{Driver: driver, Dsn: dsn, Query: query, Type: beam.EncodedType{T: t}}, imp, beam.TypeDefinition{Var: beam.XType, T: t})\n}\n\ntype queryFn struct {\n\t// Project is the project\n\tDriver string `json:\"driver\"`\n\t// Project is the project\n\tDsn string `json:\"dsn\"`\n\t// Table is the table identifier.\n\tQuery string `json:\"query\"`\n\t// Type is the encoded schema type.\n\tType beam.EncodedType `json:\"type\"`\n}\n\nfunc (f *queryFn) ProcessElement(ctx context.Context, _ []byte, emit func(beam.X)) error {\n\t//TODO move DB Open and Close to Setup and Teardown methods or StartBundle and FinishBundle\n\tdb, err := sql.Open(f.Driver, f.Dsn)\n\tif err != nil {\n\t\treturn errors.Wrapf(err, \"failed to open database: %v\", f.Driver)\n\t}\n\tdefer db.Close()\n\tstatement, err := db.PrepareContext(ctx, f.Query)\n\tif err != nil {\n\t\treturn errors.Wrapf(err, \"failed to prepare query: %v\", f.Query)\n\t}\n\tdefer statement.Close()\n\trows, err := statement.QueryContext(ctx)\n\tif err != nil {\n\t\treturn errors.Wrapf(err, \"failed to run query: %v\", f.Query)\n\t}\n\tdefer rows.Close()\n\tvar mapper rowMapper\n\tvar columns []string\n\tfor rows.Next() {\n\t\treflectRow := reflect.New(f.Type.T)\n\t\trow := reflectRow.Interface() // row : *T\n\t\tif mapper == nil {","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/io/databaseio/database.go#L57-L93","documentation":"queryFn.ProcessElement opens a database connection per element with sql.Open(f.Driver, f.Dsn). If the driver fails to initialize the connection, the error is wrapped as \"failed to open database: %v\" naming the driver. With database/sql this usually surfaces real connection problems (bad DSN, unreachable host, unknown driver) at first use.","triggerScenarios":"sql.Open(f.Driver, f.Dsn) returns an error: driver name not registered (missing import/blank import of the driver), or the driver immediately validates the DSN and fails.","commonSituations":"Typo'd driver name (\"postgres\" vs \"pgx\"); forgetting the blank import `_ \"github.com/lib/pq\"`; malformed DSN (wrong password format, missing host); container/network where the DB host is unreachable.","solutions":["Add the blank driver import, e.g. import _ \"github.com/lib/pq\" (or the driver matching f.Driver).","Verify the DSN string by connecting with psql/mysql CLI or a small standalone Go program using the same DSN.","Confirm the driver name string matches the driver's registered name exactly (e.g. \"postgres\", \"mysql\", \"sqlite3\").","Check network reachability/DNS/firewall from the worker environment to the database host."],"exampleFix":"// before\nimport \"database/sql\" // driver never registered\n\n// after\nimport (\n  \"database/sql\"\n  _ \"github.com/lib/pq\"\n)","handlingStrategy":"validation","validationCode":"db, err := sql.Open(driver, dsn)\nif err != nil { return err }\nif err := db.PingContext(ctx); err != nil { return fmt.Errorf(\"cannot reach %s: %w\", driver, err) }\ndb.Close()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always blank-import the driver package used by the pipeline.","Smoke-test the DSN with a Ping before running the job.","Store the DSN in env/config and validate it is non-empty and well-formed at startup."],"tags":["database","connection","go","sql"],"backgroundTag":"connection-refused","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}