From 6fb9b6e517e0f35c9e1222dfb0292a6cbfb2c667 Mon Sep 17 00:00:00 2001 From: Thomas Posch Date: Wed, 6 Apr 2022 11:24:03 +0200 Subject: [PATCH 1/2] 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. --- packets.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packets.go b/packets.go index ab30601ae..fab7edba2 100644 --- a/packets.go +++ b/packets.go @@ -571,9 +571,19 @@ func (mc *mysqlConn) handleErrorPacket(data []byte) error { // Error Number [16 bit uint] errno := binary.LittleEndian.Uint16(data[1:3]) + pos := 3 + + // SQL State [optional: # + 5bytes string] + var sqlstate string + if data[3] == 0x23 { + sqlstate = string(data[4 : 4+5]) + pos = 9 + } + // 1792: ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION // 1290: ER_OPTION_PREVENTS_STATEMENT (returned by Aurora during failover) - if (errno == 1792 || errno == 1290) && mc.cfg.RejectReadOnly { + // SQLSTATE 45000: user-defined exception + if (errno == 1792 || errno == 1290) && sqlstate != "45000" && mc.cfg.RejectReadOnly { // Oops; we are connected to a read-only connection, and won't be able // to issue any write statements. Since RejectReadOnly is configured, // we throw away this connection hoping this one would have write @@ -587,14 +597,6 @@ func (mc *mysqlConn) handleErrorPacket(data []byte) error { return driver.ErrBadConn } - pos := 3 - - // SQL State [optional: # + 5bytes string] - if data[3] == 0x23 { - //sqlstate := string(data[4 : 4+5]) - pos = 9 - } - // Error Message [string] return &MySQLError{ Number: errno, From a0b5ef3778a7820d7a86afda24e5da7419399b9f Mon Sep 17 00:00:00 2001 From: Thomas Posch Date: Wed, 6 Apr 2022 11:33:21 +0200 Subject: [PATCH 2/2] AUTHORS: add 'dyves labs AG' --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 876b2964a..50b9593f0 100644 --- a/AUTHORS +++ b/AUTHORS @@ -110,6 +110,7 @@ Ziheng Lyu Barracuda Networks, Inc. Counting Ltd. DigitalOcean Inc. +dyves labs AG Facebook Inc. GitHub Inc. Google Inc.