pgx icon indicating copy to clipboard operation
pgx copied to clipboard

Do not close the connection when the error does not indicate that the connection is broken.

Open YanniHu1996 opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe.

Let's say the following code in the package stdlib

func (c *Conn) Ping(ctx context.Context) error {
	if c.conn.IsClosed() {
		return driver.ErrBadConn
	}

	err := c.conn.Ping(ctx)
	if err != nil {
		// A Ping failure implies some sort of fatal state. The connection is almost certainly already closed by the
		// failure, but manually close it just to be sure.
		c.Close()
		return driver.ErrBadConn
	}

	return nil
}

The connection will be closed in case of an error returned by c.conn.Ping, however not all errors are meant to be the connection is in a fatal state. For instance, the error conn busy is caused by using the same connection concurrently, but the connection is workable when the locker is released

Describe the solution you'd like

Check the error type to determine the close operation is necessary

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

YanniHu1996 avatar Jun 05 '24 08:06 YanniHu1996

Are there any recoverable errors beside conn busy? I'd consider conn busy to be a bug in the application code.

jackc avatar Jun 05 '24 11:06 jackc