Home > Back-end >  How to force or simulate a timeout error in mysql
How to force or simulate a timeout error in mysql

Time:01-13

I tried to execute this query but after 5 seconds instead of getting a timeout error the query still get executed without error.

SET SESSION interactive_timeout = 1;
Do SLEEP(5);
SELECT 'Hello';

I also try to set the wait_timeout = 1 but i'm not getting timeout error either. Is there any other way to force a query timeout in mySQL side?

CodePudding user response:

If wait_timeout=n was set to a value less than the value passed to sleep() the server will close the connection after n seconds

If you are using workbench, you will likely not see an error, since MySQL workbench silently reconnects.

Example with command line client:

MariaDB [(none)]> set @@wait_timeout=1;

MariaDB [(none)]> select sleep(3);
ERROR 2006 (HY000): Server has gone away
No connection. Trying to reconnect...
Connection id:    34170
Current database: *** NONE ***

 ---------- 
| sleep(3) |
 ---------- 
|        0 |
 ---------- 

Usually most applications (and connectors) should have an option/setting to set read/write timeouts - so instead of CR_SERVER_GONE (2006) error you will get a CR_SERVER_LOST error (2013), which doesn't require a reconnect.

  • Related