mysql: packets.go:336: Busy buffer. Commands out of sync. Did you run multiple statements at once?
I am trying to build a web server using this package and when i try to log hits into database too quickly i get the error:
packets.go:336: Busy buffer. Commands out of sync. Did you run multiple statements at once?
My code works fine untill i make too many requests too quickly… Whats going on here?
res, err := db.Exec("INSERT INTO hits VALUES('', ?, ?, ?, ?)", ipaddress, useragent, path, timestamp)
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 24 (9 by maintainers)
Commits related to this issue
- added test helpers and tests for #314 — committed to arnehormann/mysql by arnehormann 9 years ago
ajays20078, I have a fix I am using to deal with this issue for now, check it out 😃
import “sync” var mutex = &sync.Mutex{} mutex.Lock() // … db stuff here mutex.Unlock()
Let me know your results please.
This bug is also caused by doing the following (running two queries in parallel with the same connection):
This bug was previously reported in https://github.com/go-sql-driver/mysql/issues/185#issuecomment-43951384 but there was never an issue opened for this specific bug.
@arnehormann seems to have found one situation where this is triggered, as confirmed by @rmulley:
But this seems also to happen without the usage of database/sql’s tx (transactions) API.
Can you maybe provide a minimal Go program with which you can trigger this bug? Please also report us your Go and MySQL server version and your platform (e.g. linux/amd64)
@muriarte it doesn’t work. the error is still there
if the limit is above 100, there will be the error.
Thanks @julienschmidt - your comment helped me to fix issues in my code! I had two issues caused this error:
rows.Close()call after one of the queriesI’ve added
defer rows.Close(), splitted SELECT results processing into different methods and now my code works without any error.Happening with me too. i have http api where there are multiple statements executed using a transaction object(*sql.Tx) and i get the following error: MySQL] packets.go:355: Busy buffer [MySQL] packets.go:336: Busy buffer http: panic driver: bad connection
Well, is not the ideal solution but https://github.com/go-sql-driver/mysql/issues/257 suggests to use this configuration after creating your database connection
Actually you can set MaxOpenConns to a smaller value, like 10 for example, because this configurations makes the database drivers use only one connection but properly protected to run only one query at a time. This works on my REST Api program but i think it is slower than using a connection pool, but when using a connection pool it fails often with the ‘Buffer busy’ message.
@alandroid : Before i tried this solution I used mutexes, as you suggest, to solve this problem an it worked well also, same result, same performance. 👍 But db.SetMaxIdleConns(0) is a lot cleaner, i think internally database/sql is using a mutex to protect the only connection when we use db.SetMaxIdleConns(0).
I just ran into this error, but I’ve traced it to a bug in my own code: I closed a *sql.Stmt which still had an open *sql.Rows and then tried to read the rows. Bad idea. It only misbehaves for me in a transaction, though I’m sure closing the Stmt first is a bug in any case. The exact behavior seems to depend on the query. Example:
Here’s the output from a couple of queries using an up-to-date master on Go 1.4, MySQL 5.6.17, Mac OS X 10.10.2:
https://play.golang.org/p/aWRBMSjk1z (Hope this works, and is good enough) go version: go1.4.1 windows/amd64 mysql server version: 10.0.16-MariaDB-1~trusty-log