Skip to content

Commit e2efca3

Browse files
committed
fix driver.ErrBadConn behavior
The spec says that ErrBadConn should only be returned when a connection is in a bad state. We overused it, which lead to re-executed queries in some cases. With this fix, all instances of driver.ErrBadConn are replaced with ErrInvalidConn unless they are in an exported function and appear before the network is hit. I also removed it as a return value from Close, where retrying makes no sense whatsoever.
1 parent 04cf947 commit e2efca3

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

packets.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
3131
if err != nil {
3232
errLog.Print(err)
3333
mc.Close()
34-
return nil, driver.ErrBadConn
34+
return nil, ErrInvalidConn
3535
}
3636

3737
// Packet Length [24 bit]
@@ -40,7 +40,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
4040
if pktLen < 1 {
4141
errLog.Print(ErrMalformPkt)
4242
mc.Close()
43-
return nil, driver.ErrBadConn
43+
return nil, ErrInvalidConn
4444
}
4545

4646
// Check Packet Sync [8 bit]
@@ -58,7 +58,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
5858
if err != nil {
5959
errLog.Print(err)
6060
mc.Close()
61-
return nil, driver.ErrBadConn
61+
return nil, ErrInvalidConn
6262
}
6363

6464
isLastPacket := (pktLen < maxPacketSize)
@@ -117,7 +117,7 @@ func (mc *mysqlConn) writePacket(data []byte) error {
117117
} else {
118118
errLog.Print(err)
119119
}
120-
return driver.ErrBadConn
120+
return ErrInvalidConn
121121
}
122122
}
123123

@@ -241,7 +241,7 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
241241
if data == nil {
242242
// can not take the buffer. Something must be wrong with the connection
243243
errLog.Print(ErrBusyBuffer)
244-
return driver.ErrBadConn
244+
return ErrInvalidConn
245245
}
246246

247247
// ClientFlags [32 bit]
@@ -312,7 +312,7 @@ func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error {
312312
if data == nil {
313313
// can not take the buffer. Something must be wrong with the connection
314314
errLog.Print(ErrBusyBuffer)
315-
return driver.ErrBadConn
315+
return ErrInvalidConn
316316
}
317317

318318
// Add the scrambled password [null terminated string]
@@ -334,7 +334,7 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
334334
if data == nil {
335335
// can not take the buffer. Something must be wrong with the connection
336336
errLog.Print(ErrBusyBuffer)
337-
return driver.ErrBadConn
337+
return ErrInvalidConn
338338
}
339339

340340
// Add command byte
@@ -353,7 +353,7 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
353353
if data == nil {
354354
// can not take the buffer. Something must be wrong with the connection
355355
errLog.Print(ErrBusyBuffer)
356-
return driver.ErrBadConn
356+
return ErrInvalidConn
357357
}
358358

359359
// Add command byte
@@ -374,7 +374,7 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
374374
if data == nil {
375375
// can not take the buffer. Something must be wrong with the connection
376376
errLog.Print(ErrBusyBuffer)
377-
return driver.ErrBadConn
377+
return ErrInvalidConn
378378
}
379379

380380
// Add command byte
@@ -767,7 +767,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
767767
if data == nil {
768768
// can not take the buffer. Something must be wrong with the connection
769769
errLog.Print(ErrBusyBuffer)
770-
return driver.ErrBadConn
770+
return ErrInvalidConn
771771
}
772772

773773
// command [1 byte]

statement.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type mysqlStmt struct {
2222
func (stmt *mysqlStmt) Close() error {
2323
if stmt.mc == nil || stmt.mc.netConn == nil {
2424
errLog.Print(ErrInvalidConn)
25-
return driver.ErrBadConn
25+
return ErrInvalidConn
2626
}
2727

2828
err := stmt.mc.writeCommandPacketUint32(comStmtClose, stmt.id)

0 commit comments

Comments
 (0)