I am getting the unexpected EOF and busy buffer error in go-sql-driver/mysql despite after setting the SetConnMaxLifetime, SetMaxIdleConns and SetMaxOpenConns as suggested here. Can anyone tell me the proper solution of this issue nothing seems to work for me?
db, err := sql.Open("mysql", "USERNAME:PASSWORD@tcp(IP:PORT)/DB?charset=utf8")
checkErr(err)
db.SetConnMaxLifetime(time.Second * 5)
db.SetMaxIdleConns(0)
db.SetMaxOpenConns(151)
rows, err := db.Query("Select col1, col2, col3 from tbl")
checkErr(err)
for rows.Next() {
var col1 string
var col2 int32
var col3 uint64
err = rows.Scan(&col1, &col2, &col3)
checkErr(err)
Process(col1, col2, col3)
}
According to go-sql-driver/mysql/issues/314, bad_buffer could also happen when attempting to run multiple statements within a transaction while still having an open *sql.Rows.
Ensuring that all *sql.Rows were closed before running subsequent statements resolves the issue.
rows, queryErr = tx.Query(selectQuery, queryArgs...)
// process rows.Next()
// Attempt to perform additional query
rows2, query2Err = tx.Query(selectQuery2, query2Args ...)
// Get bad_buffer error (from logs:)
// [mysql] {timestamp} packets.go:447: busy buffer
// [mysql] {timestamp} connection.go:173: bad connection
rows, queryErr = tx.Query(selectQuery, queryArgs...)
defer func(result *sql.Rows) {
_ = result.Close()
}(rows)
// process rows.Next()
// Attempt to perform additional query
rows2, query2Err = tx.Query(selectQuery2, query2Args ...)
defer func(result *sql.Rows) {
_ = result.Close()
}(rows2)
// No error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With