Mysql repeatedly calls the stored procedure problem _mysql

Source: Internet
Author: User
Tags mysql in
The problem is that, when building an application directly using the MySQL C API, a connection can only perform a single stored procedure, however, when free uses the connection again, there will be a failure to execute the Qurry, I think, such a large software will not have such a fundamental problem, After all, most of the ADODB are based on the C API, does not all software can be connected to the cache?

Don't say, there are really not many people who have access to this kind of problem. Most of the connection pool has been written, who will go directly to build the connection to operate it, so Baidu Google basically have no results, today turned around to solve this problem, suddenly have a breakthrough, found an article "on the MySQL C An analysis of the problems and solutions of API calling stored procedures is my problem.
The author's analysis is very complicated, in fact, we generally only use mysql_query, Mysql_use_result, and other functions, the crux of the problem is that when the implementation of a stored procedure, the database returned a number of data sets, even if only a data collection, And he's going to have an empty collection to end a conversation, the author scolded him metamorphosis, in fact, may be considered by the author is only a general request, MySQL is for all users to use, said can not really abnormal people to picture files and so on directly stored in the MySQL field, then the reply will not be completed once, Requires more of this network interaction, then all the interaction will definitely require a closing symbol, and the stored procedure could have returned multiple data sets, if he had only done one result in C Pai to allow the next new request, then for the same connection, the MySQL service in fact has not sent the completed data, His safe way of doing this is not to accept any new requests, until the data is sent completely, or the connection is closed, otherwise, the MySQL protocol resolution will be a problem, the next send will appear sticky packet or lost packet, so his approach is completely correct, even if only a result set, also need the current session of communication ( such as Mysql_next_result) confirmed, and then end this request, this time the server actually has no data, but this process can not be omitted, and then do not close the case of the connection will be able to make a new request.
So summed up is: the use of stored procedures must be recycled, all the results are taken to the collection, until the empty, this time the current database connection can be safely returned back, the following is a demo.
Copy Code code as follows:

Mysql_res *conn;
Mysql_res *res;
Mysql_row ROW;
conn = Connection pool. Get ("xxxx");

mysql_query (conn, "Call QT ()");

res = MYSQL_USE_RESULT (conn);
while (row = mysql_fetch_row (res)) {
Do main thins; This time, it's for the first dataset.
}
Mysql_free_result (RES);
while (res = MYSQL_NEXT_RESULT (conn))!= NULL) {
Do some thing; Looping through other data sets
Mysql_free_result (RES);
}
Connection pool. FREE (conn)

The above circular reading ensures that a connection is returned cleanly to the connection pool, while some of the connection pools may have already been disposed of in the connection pool, explaining that the rest of the dataset is taken and released. such as in PHP, such as the use of MySQL in the connection pool when you do not feel the above mentioned problems.
Related Article

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.