Today, I encountered a rare error when using Go-sql-driver to do MySQL driver to call MySQL's stored procedure. This article makes a brief summary of this error and its solution.
1 Problem Description
Following the interface provided by the SQL package in go, the code that invokes the MySQL stored procedure by the application layer code is generally as follows:
result, err: = DBH. Exec ("Call Some_procedure (?,?)", param1, param2) if err! = Nil { //error handler } //using result ...
But the value that err does not nil,err when actually executes is the error message that MySQL returns to driver:
Error 1312:procedure tcheck_db.update_vs_available can ' t return a result set in the given context
2 cause of the problem
This problem occurs because, when the stored procedure is called, MySQL expects the client's connection type to be multi-statement mode, but go-sql-driver this package does not currently have this connection mode set, so MySQL returns 1 errors.
In fact, when using go-sql-driver, there are some other limitations that can be seen in go-database-sql-surprises, in addition to the inability to invoke stored procedures.
The question was also reported in Go-sql-driver's discussion area, see here.
3 Problem Solving
Now that we have found the cause, that is the right remedy, can think of the way is to connect go-sql-driver MySQL connection mode is set to Multi-statement. The method is:
1) Go get github.com/go-sql-driver/mysql Download Dependency Package
2) Edit the file Src/github.com/go-sql-driver/mysql/packets.go file in function writeauthpacket (cipher []byte) Add the following two flags at the client flag (approximately 210 rows):
Clientmultistatements and Clientmultiresults
The following code is added:
Func (MC *mysqlconn) Writeauthpacket (cipher []byte) error { //Adjust client flags based on server support clientf Lags: = clientProtocol41 | Clientsecureconn | Clientlongpassword | clienttransactions | Clientlocalfiles | clientmultistatements | Add this line clientmultiresults | Add this line again Mc.flags&clientlongflag if mc.cfg.clientFoundRows { clientflags |= clientfoundrows } ...
3,rebuild your code that relies on Go-sql-driver.
Ok. After the three steps above, when you execute the stored procedure Code in 1, you will not get an error.
Golang troubleshooting when invoking a MySQL stored procedure with go-sql-driver