mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
database/sql: clarify that DB.Prepare's stmt is safe for concurrent use
And add a test too, for Alex. :) Fixes #3734 R=golang-dev, adg CC=golang-dev https://golang.org/cl/7399046
This commit is contained in:
parent
4036d876ea
commit
c53fab969c
2 changed files with 32 additions and 1 deletions
|
|
@ -408,7 +408,9 @@ func (db *DB) putConn(c driver.Conn, err error) {
|
|||
c.Close()
|
||||
}
|
||||
|
||||
// Prepare creates a prepared statement for later execution.
|
||||
// Prepare creates a prepared statement for later queries or executions.
|
||||
// Multiple queries or executions may be run concurrently from the
|
||||
// returned statement.
|
||||
func (db *DB) Prepare(query string) (*Stmt, error) {
|
||||
var stmt *Stmt
|
||||
var err error
|
||||
|
|
|
|||
|
|
@ -273,6 +273,35 @@ func TestStatementQueryRow(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
// golang.org/issue/3734
|
||||
func TestStatementQueryRowConcurrent(t *testing.T) {
|
||||
db := newTestDB(t, "people")
|
||||
defer closeDB(t, db)
|
||||
stmt, err := db.Prepare("SELECT|people|age|name=?")
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare: %v", err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
const n = 10
|
||||
ch := make(chan error, n)
|
||||
for i := 0; i < n; i++ {
|
||||
go func() {
|
||||
var age int
|
||||
err := stmt.QueryRow("Alice").Scan(&age)
|
||||
if err == nil && age != 1 {
|
||||
err = fmt.Errorf("unexpected age %d", age)
|
||||
}
|
||||
ch <- err
|
||||
}()
|
||||
}
|
||||
for i := 0; i < n; i++ {
|
||||
if err := <-ch; err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// just a test of fakedb itself
|
||||
func TestBogusPreboundParameters(t *testing.T) {
|
||||
db := newTestDB(t, "foo")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue