Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database/sql: defer db.putConn(ci, err) pattern, err is always nil #3950

Closed
gopherbot opened this issue Aug 14, 2012 · 2 comments
Closed

database/sql: defer db.putConn(ci, err) pattern, err is always nil #3950

gopherbot opened this issue Aug 14, 2012 · 2 comments

Comments

@gopherbot
Copy link

by pcrosby:

In database/sql/sql.go, all the calls to db.putConn that use a defer will always pass
nil for the error.

For example, assuming the following exec function gets to the "defer db.putConn(ci,
err)" line, err will always be nil, no matter what happens to err after the
"defer..." line (since the arguments to the deferred function are evaluated
when the defer executes, not when the call executes).

I know putConn only cares if err is a driver.ErrBadConn, but it seems to me like that is
a possible error in many of the functions in sql.go that use this defer db.putConn(ci,
err) pattern.

func (db *DB) exec(query string, sargs []driver.Value) (res Result, err error) {
    ci, err := db.conn()
    if err != nil {
        return nil, err
    }
    defer db.putConn(ci, err)

    if execer, ok := ci.(driver.Execer); ok {
        resi, err := execer.Exec(query, sargs)
        if err != driver.ErrSkip {
            if err != nil {
                return nil, err
            }
            return result{resi}, nil
        }
    }

    sti, err := ci.Prepare(query)
    if err != nil {
        return nil, err
    }
    defer sti.Close()

    resi, err := sti.Exec(sargs)
    if err != nil {
        return nil, err
    }
    return result{resi}, nil
}
@gopherbot
Copy link
Author

Comment 1 by pcrosby:

Sorry, this is a duplicate of 3777.  Please disregard.

@ianlancetaylor
Copy link
Contributor

Comment 2:

Status changed to Duplicate.

Merged into issue #3777.

@mikioh mikioh changed the title database/sql defer db.putConn(ci, err) pattern, err is always nil database/sql: defer db.putConn(ci, err) pattern, err is always nil Feb 18, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants