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: improve documentation for closing prepared statement on Tx #40280

Open
thiagoretondar opened this issue Jul 18, 2020 · 1 comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@thiagoretondar
Copy link

The https://golang.org/pkg/database/sql/#Tx documentation says:

The statements prepared for a transaction by calling the transaction's Prepare or Stmt methods are closed by the call to Commit or Rollback.

But in the example of https://golang.org/pkg/database/sql/#Tx.Prepare there's a comment ("Prepared statements take up server resources and should be closed after use.") that can cause some doubts about how to handle it

package main

import (
	"context"
	"database/sql"
	"log"
)

var (
	ctx context.Context
	db  *sql.DB
)

func main() {
	projects := []struct {
		mascot  string
		release int
	}{
		{"tux", 1991},
		{"duke", 1996},
		{"gopher", 2009},
		{"moby dock", 2013},
	}

	tx, err := db.Begin()
	if err != nil {
		log.Fatal(err)
	}
	defer tx.Rollback() // The rollback will be ignored if the tx has been committed later in the function.

	stmt, err := tx.Prepare("INSERT INTO projects(id, mascot, release, category) VALUES( ?, ?, ?, ? )")
	if err != nil {
		log.Fatal(err)
	}
	defer stmt.Close() // Prepared statements take up server resources and should be closed after use.

	for id, project := range projects {
		if _, err := stmt.Exec(id+1, project.mascot, project.release, "open source"); err != nil {
			log.Fatal(err)
		}
	}
	if err := tx.Commit(); err != nil {
		log.Fatal(err)
	}
}
@toothrot toothrot changed the title sql.Tx - close prepared statement database/sql: improve documentation for closing prepared statement on Tx Jul 21, 2020
@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 21, 2020
@toothrot toothrot added this to the Backlog milestone Jul 21, 2020
@toothrot
Copy link
Contributor

/cc @bradfitz @kardianos

I believe the documentation is more relevant when preparing a statement outside of a transaction, even though this example is specifically for (*Tx).Prepare(). Noting that they use additional resources on the server, even within the scope of a transaction, seems relevant to me.

@kardianos kardianos self-assigned this Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants