Skip to content

Commit 52c1917

Browse files
authored
remove unnecessary logs (#1599)
Logging ErrInvalidConn when the connection already closed doesn't provide any help to users. Additonally, database/sql now uses Validator() to check connection liveness before calling query methods. So stop using `mc.log(ErrInvalidConn)` idiom. This PR includes some cleanup and documentation relating to `mc.markBadConn()`.
1 parent 2f69712 commit 52c1917

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

connection.go

+9-12
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,13 @@ func (mc *mysqlConn) handleParams() (err error) {
111111
return
112112
}
113113

114+
// markBadConn replaces errBadConnNoWrite with driver.ErrBadConn.
115+
// This function is used to return driver.ErrBadConn only when safe to retry.
114116
func (mc *mysqlConn) markBadConn(err error) error {
115-
if mc == nil {
116-
return err
117-
}
118-
if err != errBadConnNoWrite {
119-
return err
117+
if err == errBadConnNoWrite {
118+
return driver.ErrBadConn
120119
}
121-
return driver.ErrBadConn
120+
return err
122121
}
123122

124123
func (mc *mysqlConn) Begin() (driver.Tx, error) {
@@ -127,7 +126,6 @@ func (mc *mysqlConn) Begin() (driver.Tx, error) {
127126

128127
func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {
129128
if mc.closed.Load() {
130-
mc.log(ErrInvalidConn)
131129
return nil, driver.ErrBadConn
132130
}
133131
var q string
@@ -189,7 +187,6 @@ func (mc *mysqlConn) error() error {
189187

190188
func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
191189
if mc.closed.Load() {
192-
mc.log(ErrInvalidConn)
193190
return nil, driver.ErrBadConn
194191
}
195192
// Send command
@@ -324,7 +321,6 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
324321

325322
func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
326323
if mc.closed.Load() {
327-
mc.log(ErrInvalidConn)
328324
return nil, driver.ErrBadConn
329325
}
330326
if len(args) != 0 {
@@ -384,7 +380,6 @@ func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error)
384380
handleOk := mc.clearResult()
385381

386382
if mc.closed.Load() {
387-
mc.log(ErrInvalidConn)
388383
return nil, driver.ErrBadConn
389384
}
390385
if len(args) != 0 {
@@ -408,7 +403,7 @@ func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error)
408403
var resLen int
409404
resLen, err = handleOk.readResultSetHeaderPacket()
410405
if err != nil {
411-
return nil, mc.markBadConn(err)
406+
return nil, err
412407
}
413408

414409
rows := new(textRows)
@@ -482,7 +477,6 @@ func (mc *mysqlConn) finish() {
482477
// Ping implements driver.Pinger interface
483478
func (mc *mysqlConn) Ping(ctx context.Context) (err error) {
484479
if mc.closed.Load() {
485-
mc.log(ErrInvalidConn)
486480
return driver.ErrBadConn
487481
}
488482

@@ -704,3 +698,6 @@ func (mc *mysqlConn) ResetSession(ctx context.Context) error {
704698
func (mc *mysqlConn) IsValid() bool {
705699
return !mc.closed.Load()
706700
}
701+
702+
var _ driver.SessionResetter = &mysqlConn{}
703+
var _ driver.Validator = &mysqlConn{}

errors.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var (
3232

3333
// errBadConnNoWrite is used for connection errors where nothing was sent to the database yet.
3434
// If this happens first in a function starting a database interaction, it should be replaced by driver.ErrBadConn
35-
// to trigger a resend.
35+
// to trigger a resend. Use mc.markBadConn(err) to do this.
3636
// See https://github.com/go-sql-driver/mysql/pull/302
3737
errBadConnNoWrite = errors.New("bad connection")
3838
)

statement.go

-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ func (stmt *mysqlStmt) CheckNamedValue(nv *driver.NamedValue) (err error) {
5151

5252
func (stmt *mysqlStmt) Exec(args []driver.Value) (driver.Result, error) {
5353
if stmt.mc.closed.Load() {
54-
stmt.mc.log(ErrInvalidConn)
5554
return nil, driver.ErrBadConn
5655
}
5756
// Send command
@@ -95,7 +94,6 @@ func (stmt *mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {
9594

9695
func (stmt *mysqlStmt) query(args []driver.Value) (*binaryRows, error) {
9796
if stmt.mc.closed.Load() {
98-
stmt.mc.log(ErrInvalidConn)
9997
return nil, driver.ErrBadConn
10098
}
10199
// Send command

0 commit comments

Comments
 (0)