Oneb
Oneb

Reputation: 395

Setting the MySql wait_timeout session variable

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

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.

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..

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 ).

    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.

What should i do to replicate the error?

Upvotes: 3

Views: 6701

Answers (1)

Devart
Devart

Reputation: 122032

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.

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

Upvotes: 3

Related Questions