mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
database/sql: associate a mutex with each driver interface
The database/sql/driver docs make this promise: "Conn is a connection to a database. It is not used concurrently by multiple goroutines." That promises exists as part of database/sql's overall goal of making drivers relatively easy to write. So far this promise has been kept without the use of locks by being careful in the database/sql package, but sometimes too careful. (cf. golang.org/issue/3857) The CL associates a Mutex with each driver.Conn, and with the interface value progeny thereof. (e.g. each driver.Tx, driver.Stmt, driver.Rows, driver.Result, etc) Then whenever those interface values are used, the Locker is locked. This CL should be a no-op (aside from some new Lock/Unlock pairs) and doesn't attempt to fix Issue 3857 or Issue 4459, but should make it much easier in a subsequent CL. Update #3857 R=golang-dev, adg CC=golang-dev https://golang.org/cl/7803043
This commit is contained in:
parent
eb80431b61
commit
f28c8fba67
3 changed files with 219 additions and 113 deletions
|
|
@ -5,7 +5,6 @@
|
|||
package sql
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
|
@ -16,10 +15,10 @@ import (
|
|||
func init() {
|
||||
type dbConn struct {
|
||||
db *DB
|
||||
c driver.Conn
|
||||
c *driverConn
|
||||
}
|
||||
freedFrom := make(map[dbConn]string)
|
||||
putConnHook = func(db *DB, c driver.Conn) {
|
||||
putConnHook = func(db *DB, c *driverConn) {
|
||||
for _, oc := range db.freeConn {
|
||||
if oc == c {
|
||||
// print before panic, as panic may get lost due to conflicting panic
|
||||
|
|
@ -78,7 +77,7 @@ func numPrepares(t *testing.T, db *DB) int {
|
|||
if n := len(db.freeConn); n != 1 {
|
||||
t.Fatalf("free conns = %d; want 1", n)
|
||||
}
|
||||
return db.freeConn[0].(*fakeConn).numPrepare
|
||||
return db.freeConn[0].ci.(*fakeConn).numPrepare
|
||||
}
|
||||
|
||||
func TestQuery(t *testing.T) {
|
||||
|
|
@ -576,7 +575,7 @@ func TestQueryRowClosingStmt(t *testing.T) {
|
|||
if len(db.freeConn) != 1 {
|
||||
t.Fatalf("expected 1 free conn")
|
||||
}
|
||||
fakeConn := db.freeConn[0].(*fakeConn)
|
||||
fakeConn := db.freeConn[0].ci.(*fakeConn)
|
||||
if made, closed := fakeConn.stmtsMade, fakeConn.stmtsClosed; made != closed {
|
||||
t.Errorf("statement close mismatch: made %d, closed %d", made, closed)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue