Skip to content

database/sql: document what to do on DB.Ping error #23738

Closed

Description

@pjebs

The documentation doesn't make clear what to do if we get an error "bad connection".

When Ping returns an error, is it advisable to close the DB object and create a new DB object to replace the old one?

After all, if Ping returns an error, something is clearly wrong.
Is it only the connection that is bad or the pool.

This is how a lot of people are implementing it: https://github.com/pjebs/go-skeleton/blob/master/app/providers/database.go#L61

Activity

  1. changed the title [-]closing DB after Ping returns an error[/-] [+]database/sql: document what to do on DB.Ping error[/+] on Feb 8, 2018
  2. added this to the Go1.11 milestone on Feb 8, 2018
  3. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Feb 8, 2018
  4. kardianos commented on Feb 8, 2018

    @kardianos
    Contributor

    I'm not exactly sure what you mean by a "bad pool". How would you tell the difference between an incorrect DSN and a temporary network problem? I think retrying or closing the pool could both be correct.

    Glancing at the code you linked concerns me the author is confusing a DB pool with a single connection.

    I'm not convinced the docs need clarification, but I can look into it. Maybe we can discuss the pros and cons of each approch, maybe provide a recommendation. There is a direct tie here with how an application comes online (like needing a healthz endpoint or windows service).

  5. AlekSi commented on Feb 20, 2018

    @AlekSi
    Contributor

    I'm not convinced the docs need clarification

    As an author of one Go SQL driver, author of one ORM-ish package, and contributor to numerous other SQL-related Go package, I can say that database/sql and database/sql/driver documentation can definitely be improved. Specifically, it would be very helpful if documentation for more or all methods and interfaces in database/sql/driver mention how returned errors are handled by database/sql internals. I don't think that would completely eliminate a need to dig into the code, but at least that documentation can be an explicit contract between the library and third-party drivers.

    Sorry if it is not very specific. I will create separete specific issues and/or CL when I encounter those issues again.

  6. kardianos commented on Mar 16, 2018

    @kardianos
    Contributor

    @AlekSi From what I can see, database/sql/driver does a good job explaining what special errors can be returned by interfaces.

    However, until go1.10 with the SessionResetter interface, there was no good way to expose normal database errors to users without poisoning the connection pool. Now the story is simple: return the normal custom or wrapped network database error to the user. Then when the ResetSession on the connection is called, return driver.ErrBadConn.

    I would love to have a conversation on another issue or https://groups.google.com/forum/#!forum/golang-sql about ways we can improve documentation in specific ways (or find an existing issue). One thing that would probably be helpful is to provide a bare-bones "real" database driver for others to inspect.

  7. gopherbot commented on Mar 16, 2018

    @gopherbot
    Contributor

    Change https://golang.org/cl/101216 mentions this issue: database/sql: add more complete examples for opening a DB

  8. modified the milestones: Go1.11, Go1.12 on Jun 29, 2018
  9. added a commit that references this issue on Nov 21, 2018
    8d33508
  10. locked and limited conversation to collaborators on Nov 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions