简体   繁体   English

MySQL服务器端超时

[英]MySQL server-side timeout

I have some connection code that causes a timeout when queries take too long. 我有一些连接代码,当查询花费太长时间时会导致超时。 The connection options are setup like this ( timeout is an integer): 连接选项的设置如下( timeout为整数):

sql::ConnectOptionsMap com;
com["hostName"] = url; // Some string
com["userName"] = user; // Some string
com["password"] = pwd; // Some string
com["OPT_RECONNECT"] = true;
com["OPT_READ_TIMEOUT"] = timeout; // Usually 1 (second)
com["OPT_WRITE_TIMEOUT"] = timeout; // Usually 1 (second)

After testing the timeout setup above what I found is that a throw does occur but MySQL continues trying to execute the query. 在测试超时设置之后,我发现确实发生了抛出异常,但是MySQL继续尝试执行查询。 In other words, the below try goes to the catch after the configured timeout with error code 2013 but it doesn't stop MySQL from trying to execute the query (2013 is an error code related to lost connection): 换句话说,下面的try在错误代码为2013的配置超时后进入catch ,但它不会阻止MySQL尝试执行查询(2013是与连接丢失有关的错误代码):

// Other code

try
{
    stmt = con->createStatement();

    stmt->execute("DROP DATABASE IF EXISTS MySQLManagerTest_TimeoutRead");
    stmt->execute("CREATE DATABASE MySQLManagerTest_TimeoutRead");

    stmt->execute("USE MySQLManagerTest_TimeoutRead");
    stmt->execute("CREATE TABLE foo (bar INT)");

    for (int i = 0; i < 100; i++)
        stmt->execute("INSERT INTO foo (bar) VALUES (" + LC(i) + ")");


    // A bit of playing is needed in the loop condition
    // Make it longer than a second but not too long
    // Using 10000 seems to take roughly 5 seconds

    stmt->execute(
        "CREATE FUNCTION waitAWhile() "
            "RETURNS INT READS SQL DATA "
        "BEGIN "
            "DECLARE baz INT DEFAULT 0; "
            "WHILE baz < 10000 DO "
                "SET baz = baz + 1; "
            "END WHILE; "
            "RETURN baz; "
        "END;"
    );

    res = stmt->executeQuery("SELECT 1 FROM foo WHERE bar = waitAWhile()");

} catch (sql::SQLException &e) {
    std::cout << e.getErrorCode() << std::endl;
}

// Other code

I was able to notice that MySQL did not stop by running "top" at the same time as the above testing code. 我能够注意到MySQL并没有通过与上面的测试代码同时运行“ top”而停止。 Making the above MySQL waitAWhile() function instead be an infinite loop further confirmed that MySQL was not stopping because I had to kill the MySQL process to make it stop 使上面的MySQL waitAWhile()函数成为一个无限循环,这进一步确认了MySQL没有停止,因为我必须杀死MySQL进程使其停止

This kind of timeout is not what I wanted, I wanted MySQL to give up on a query if it took too long. 这种超时不是我想要的,我希望MySQL如果花费太长时间就放弃查询。 Can this be done (so that both my execution and MySQL stop doing work)? 可以做到这一点(以便我的执行和MySQL都停止工作)吗? Additionally, can this be specified only for INSERT queries? 此外,是否只能为INSERT查询指定?

You can do it in regular SQL by having SQL Server set a max query execution time. 您可以通过让SQL Server设置最大查询执行时间来在常规SQL中执行此操作。 However, it doesn't look like MySQL supports this; 但是,MySQL似乎并不支持这一点。 see the following accepted SO answer for more details: MySQL - can I limit the maximum time allowed for a query to run? 有关更多详细信息,请参见以下接受的SO答案: MySQL-我可以限制查询运行的最大时间吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM