Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pilat's avatar
Level 41

Setting wait_timeout for MySQL connection

Hi, I need to set wait_timeout session variable for the database connection. BUt:

  1. It has to be done inside my application because the hosting team refuses to change this option "for all of the users". They say I can do it easily by issuing the following query: SET SESSION wait_timeout = XXXXXX.

  2. I need to find a proper place in which to issue this query. As I understand, it should be issued somewhere near connection establishing, is it right? There is no such option in config/database.php and I shouldn't touch files inside vendor/, off cause. Besides, this adjustment should be indexed by git.

So, could you advise a proper way to inject this?

0 likes
3 replies
bobbybouwmann's avatar
Level 88

You can use something like this

DB::statement("SET SESSION time_zone = 'xxxx'");

You might even do this statement in your AppServiceProvider ;)

1 like
pilat's avatar
Level 41

@THECODEMILL - Have to revisit this question for the same reason. I have some routes that do not need to consult database while serving (the data is taken from cache, etc.) and having this DB::statement() in the AppServiceProvider forces the DB connection to be established regardless…

mcgregorc's avatar

If you only require it on certain routes I would suspect this is a good use-case to put it in middleware and assign the middleware to the affected routes.

Please or to participate in this conversation.