MySQL query time-out available?
Have a look at this link from stackoverflow. It may not meet all your requirements (stated above), but I think it will help.
If you specify which programming language you are using to connect to MySQL server, you may find a better way to do it.