设置MySql wait_timeout会话变量

时间:2022-08-11 01:39:34

I am currently working in a java program involving mysql. I encountered a problem with these message:

我目前正在从事涉及mysql的java程序。我遇到这些消息的问题:

Communications link failure. The last packet successfully received from the server was 174,165,153 milliseconds ago. The last packet sent successfully to the server was 4 milliseconds ago.

通信链路故障。从服务器成功收到的最后一个数据包是174,165,153毫秒。成功发送到服务器的最后一个数据包是4毫秒前。

I believe this has to do with the session variable 'wait_timeout'. My assumption could be wrong though so I am confirming it here. Is this really because of the 'wait_timeout' session variable? Does this error mean that my database connection is "expired" in a sense that the session timeout already lapsed? If yes, I have another problem.. I cannot reproduce the said error..

我相信这与会话变量'wait_timeout'有关。我的假设可能是错的,所以我在这里确认了。这真的是因为'wait_timeout'会话变量吗?在会话超时已经失效的意义上,此错误是否意味着我的数据库连接“已过期”?如果是,我有另一个问题..我无法重现所述错误..

This is what I am trying in order to replicate the error.

这是我正在尝试复制错误。

  1. Setting the wait_timeout variable to 5 which is originally 28800( 8 hrs ).

    将wait_timeout变量设置为5,最初为28800(8小时)。

    mysql> set session wait_timeout=5;

    mysql> set session wait_timeout = 5;

  2. Thinking that my next attempt to access database would apply this, I would run this code:

    想到我下次尝试访问数据库会应用这个,我会运行这段代码:

        Class.forName("com.mysql.jdbc.Driver");
        oConnection = DriverManager.getConnection(sUrl,sUser,sPass);
        System.out.println("Database Connection Established.\n");
        Thread.sleep(6000);
    
        executeSimpleQuery();
    

    The executeSimpleQuery() function just executes a simple SELECT * FROM statement to check if the connection is still working. Apparently, if the wait_timeout is just 5 and I've made the thread sleep for 6 seconds, the connection would now be expired and the error should be produced. But, the expected result does not happen instead it performs the query.

    executeSimpleQuery()函数只执行一个简单的SELECT * FROM语句来检查连接是否仍然有效。显然,如果wait_timeout只是5并且我让线程休眠了6秒,那么连接现在将过期并且应该产生错误。但是,预期的结果不会发生,而是执行查询。

What should i do to replicate the error?

我该怎么做才能复制错误?

1 个解决方案

#1


2  

You set wait_timeout=5 for current session; then opened another session (in java application) and executed the query. Try to set this variable after the opening connection, or set it globally.

你为当前会话设置wait_timeout = 5;然后打开另一个会话(在java应用程序中)并执行查询。尝试在打开连接后设置此变量,或全局设置它。

For example -

例如 -

SET @@global.wait_timeout = 15; -- Set global variable
SET @@local.wait_timeout = 25; -- Set session variable
SELECT @@global.wait_timeout, @@local.wait_timeout; -- Check global and session variables

#1


2  

You set wait_timeout=5 for current session; then opened another session (in java application) and executed the query. Try to set this variable after the opening connection, or set it globally.

你为当前会话设置wait_timeout = 5;然后打开另一个会话(在java应用程序中)并执行查询。尝试在打开连接后设置此变量,或全局设置它。

For example -

例如 -

SET @@global.wait_timeout = 15; -- Set global variable
SET @@local.wait_timeout = 25; -- Set session variable
SELECT @@global.wait_timeout, @@local.wait_timeout; -- Check global and session variables