ResetSession(ctx context.Context) error
}
-// ConnectionValidator may be implemented by Conn to allow drivers to
+// Validator may be implemented by Conn to allow drivers to
// signal if a connection is valid or if it should be discarded.
//
// If implemented, drivers may return the underlying error from queries,
// even if the connection should be discarded by the connection pool.
-type ConnectionValidator interface {
- // ValidConnection is called prior to placing the connection into the
+type Validator interface {
+ // IsValid is called prior to placing the connection into the
// connection pool. The connection will be discarded if false is returned.
- ValidConnection() bool
+ IsValid() bool
}
// Result is the result of a query execution.
return nil
}
-var _ driver.ConnectionValidator = (*fakeConn)(nil)
+var _ driver.Validator = (*fakeConn)(nil)
-func (c *fakeConn) ValidConnection() bool {
+func (c *fakeConn) IsValid() bool {
return !c.isBad()
}
if needsReset {
dc.needReset = true
}
- if cv, ok := dc.ci.(driver.ConnectionValidator); ok {
- return cv.ValidConnection()
+ if cv, ok := dc.ci.(driver.Validator); ok {
+ return cv.IsValid()
}
return true
}
}
}
+// TestConnIsValid verifies that a database connection that should be discarded,
+// is actually discarded and does not re-enter the connection pool.
+// If the IsValid method from *fakeConn is removed, this test will fail.
+func TestConnIsValid(t *testing.T) {
+ db := newTestDB(t, "people")
+ defer closeDB(t, db)
+
+ db.SetMaxOpenConns(1)
+
+ ctx := context.Background()
+
+ c, err := db.Conn(ctx)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = c.Raw(func(raw interface{}) error {
+ dc := raw.(*fakeConn)
+ dc.stickyBad = true
+ return nil
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ c.Close()
+
+ if len(db.freeConn) > 0 && db.freeConn[0].ci.(*fakeConn).stickyBad {
+ t.Fatal("bad connection returned to pool; expected bad connection to be discarded")
+ }
+}
+
// Tests fix for issue 2542, that we release a lock when querying on
// a closed connection.
func TestIssue2542Deadlock(t *testing.T) {