File tree 1 file changed +19
-11
lines changed
1 file changed +19
-11
lines changed Original file line number Diff line number Diff line change @@ -143,17 +143,25 @@ func (mc *mysqlConn) writePacket(ctx context.Context, data []byte) error {
143
143
}
144
144
data [3 ] = mc .sequence
145
145
146
- // check the connection is still alive
147
- select {
148
- case <- mc .readRes :
149
- mc .closeContext (ctx )
150
- return errBadConnNoWrite
151
- case <- mc .closech :
152
- return ErrInvalidConn
153
- case <- ctx .Done ():
154
- mc .cleanup ()
155
- return ctx .Err ()
156
- default :
146
+ // Perform a stale connection check. We only perform this check for
147
+ // the first query on a connection that has been checked out of the
148
+ // connection pool: a fresh connection from the pool is more likely
149
+ // to be stale, and it has not performed any previous writes that
150
+ // could cause data corruption, so it's safe to return ErrBadConn
151
+ // if the check fails.
152
+ if mc .reset {
153
+ mc .reset = false
154
+ select {
155
+ case <- mc .readRes :
156
+ mc .closeContext (ctx )
157
+ return errBadConnNoWrite
158
+ case <- mc .closech :
159
+ return ErrInvalidConn
160
+ case <- ctx .Done ():
161
+ mc .cleanup ()
162
+ return ctx .Err ()
163
+ default :
164
+ }
157
165
}
158
166
159
167
// request writing the packet
You can’t perform that action at this time.
0 commit comments