Golang troubleshooting when invoking a MySQL stored procedure with go-sql-driver

Source: Internet
Author: User
Tags mysql download

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.