MySQL C API - 在调用mysql_store_result()或mysql_stmt_store_result()之后重用连接

时间:2013-03-08 16:10:00

标签: mysql mysql++ libmysql

对于“选择”sql,在我们通过调用my_fetch_xxx()API复制所有行之前,重复使用连接调用my_xxxx_store()API(以缓存客户端中的所有结果)是安全的吗?通过调用my_xxxx_store()API缓存的结果集?

例如,如果通过传递不同的select语句从多个线程调用,此函数是否正确获取数据,没有任何问题。 喜欢同时打电话

From thread-1: foo("select * from table1");
From thread-2: foo("select * from table2");
From thread-3: foo("select * from table3");
etc....

函数foo就像。

/* Assume that "connection" is a valid properly initialiaed mysql connection handle global variabale*/
/* Assume that there is only one result set. (Means that we dont need to call mysql_next_result() ) */
foo( char *sqlStatement )
{
    threadLock();
    mysql_real_query( connection, sqlStatement, ... );
    MYSQL_RES* result = mysql_store_result(connection);
    threadUnLock();
    /* The "connection" can be reused now, since all data is cached in client. */

    while( more data rows )
    {
        MYSQL_ROW row = mysql_fetch_row(result);
        /*
           Copy data;
        */
    }
    mysql_free_result(result);
}

准备好的陈述也是安全的吗?像。的东西。

bar( char *sqlStatement, char *params[] )
{
    threadLock();
    MYSQL_STMT *stmt = mysql_stmt_init(connection);
    mysql_stmt_prepare(stmt, sqlStatement, ...);
    int paramCount= mysql_stmt_param_count(stmt);
    MYSQL_RES *metaResult = mysql_stmt_result_metadata(stmt);
    int columnCount= mysql_num_fields(metaResult)
    mysql_stmt_execute(stmt);
    mysql_stmt_bind_result(stmt, ....)
    mysql_stmt_store_result(stmt);
    threadUnLock();
    /* The "connection" can be reused now, since all data is cached in client. */

    while( more data rows )
    {
        MYSQL_ROW row = mysql_stmt_fetch(stmt);
        /*
           Copy data;
        */
    }
    mysql_free_result(metaResult);
    mysql_stmt_free_result(stmt);
    mysql_stmt_close(stmt);
}

0 个答案:

没有答案