Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate "MYSQL has gone away" error?

Tags:

php

mysql

I'm running lots of cron job and once in a while there is a MYSQL has gone away error.

I've now written some code to handle the error, but how do I simulate the error on my localhost so that the code can be tested thoroughly?

like image 698
Yeti Avatar asked May 08 '10 09:05

Yeti


People also ask

What does MySQL server has gone away mean?

The MySQL server has gone away error, means that MySQL server (mysqld) timed out and closed the connection. By default, MySQL will close connections after eight hours (28800 seconds) if nothing happens.


3 Answers

If it was on another machine you could unplug the network cable!

like image 25
Toby Allen Avatar answered Oct 15 '22 18:10

Toby Allen


Kill a long-running thread.

See: KILL syntax

The documentation about that error also lists different causes, so you could emulate some of them (eg: change the timeout to be very low, etc).

like image 81
nickf Avatar answered Oct 15 '22 17:10

nickf


Mock the (affected method in the) Database adapter and have it raise the error. That's the usual approach when unit-testing code that has dependencies on external resources. If you are not using PHPUnit yet, this is a great opportunity to get started with it.

Further reading

  • http://en.wikipedia.org/wiki/Mock_object
  • http://www.phpunit.de/
  • http://www.phpunit.de/manual/current/en/test-doubles.html#test-doubles.mock-objects
like image 39
Gordon Avatar answered Oct 15 '22 19:10

Gordon