Skip to content

Commit 6fb9b6e

Browse files
committed
Ignore SQLSTATE '45000' when RejectReadOnly is set
SQLSTATE '45000' is for user-defined exceptions. Their errno can collide with system exceptions however. Don't close the connection on user exceptions.
1 parent eff3908 commit 6fb9b6e

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

packets.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,19 @@ func (mc *mysqlConn) handleErrorPacket(data []byte) error {
571571
// Error Number [16 bit uint]
572572
errno := binary.LittleEndian.Uint16(data[1:3])
573573

574+
pos := 3
575+
576+
// SQL State [optional: # + 5bytes string]
577+
var sqlstate string
578+
if data[3] == 0x23 {
579+
sqlstate = string(data[4 : 4+5])
580+
pos = 9
581+
}
582+
574583
// 1792: ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION
575584
// 1290: ER_OPTION_PREVENTS_STATEMENT (returned by Aurora during failover)
576-
if (errno == 1792 || errno == 1290) && mc.cfg.RejectReadOnly {
585+
// SQLSTATE 45000: user-defined exception
586+
if (errno == 1792 || errno == 1290) && sqlstate != "45000" && mc.cfg.RejectReadOnly {
577587
// Oops; we are connected to a read-only connection, and won't be able
578588
// to issue any write statements. Since RejectReadOnly is configured,
579589
// we throw away this connection hoping this one would have write
@@ -587,14 +597,6 @@ func (mc *mysqlConn) handleErrorPacket(data []byte) error {
587597
return driver.ErrBadConn
588598
}
589599

590-
pos := 3
591-
592-
// SQL State [optional: # + 5bytes string]
593-
if data[3] == 0x23 {
594-
//sqlstate := string(data[4 : 4+5])
595-
pos = 9
596-
}
597-
598600
// Error Message [string]
599601
return &MySQLError{
600602
Number: errno,

0 commit comments

Comments
 (0)