M
M
mitaichik2015-12-09 12:42:23
Yii
mitaichik, 2015-12-09 12:42:23

How to set SQL in the config, which is filled after the creation of the DbConnection?

Hello!
I'm running php workers. For them, after connecting to the database, you need to run

SET wait_timeout = 259200;
SET interactive_timeout = 259200;

so that they do not fall when they stand without action.
I would like to somehow set this in the application config, so as not to override DbConnection::init, but I don’t understand how to do it.
Is there such a possibility?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korovin, 2015-12-09
@mitaichik

Good afternoon. Try this: there are several triggers in yii\db\Connection, you definitely need to hook on EVENT_AFTER_OPEN.
Something along the lines of:

...
'db' => [
  'class' => 'yii\db\Connection',
  'dsn' => 'mysql:host=localhost;dbname=mydb',
  'username' => 'myname',
  'password' => 'mypassword',
  'charset' => 'utf8',
  'on ' . yii\db\Connection::EVENT_AFTER_OPEN => function () {
        Yii::$app->db->createCommand("SET wait_timeout = 259200")->execute();
    Yii::$app->db->createCommand("SET interactive_timeout = 259200")->execute();
   },
]
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question