udokmeci/yii2-beanstalk

SQLSTATE[HY000] [1040] Too many connections

lordlele opened this issue · 2 comments

Hi,
I didn't understand the purpose of setDBSessionTimeout() but that function never close MySQL connection so in case of many jobs, they start to fail because MySQL answer:

SQLSTATE[HY000] [1040] Too many connections

I mean, this code here, is really doing something useful?

if ($this->_lasttimereconnect == null) {
$this->_lasttimereconnect = time();
$this->setDBSessionTimeout();
}
if (time() - $this->_lasttimereconnect > 60 * 60) {
$this->getDb()->close();
$this->getDb()->open();
Yii::info(Yii::t('udokmeci.beanstalkd', "Reconnecting to the DB"));
$this->setDBSessionTimeout();
$this->_lasttimereconnect = time();
}

For what I can see, that code only handle a timer, but that timer will be never used outside of that piece of code... but maybe I'm missing something.

If you think that code was not needed, I'll be happy to submit a PR.

Hi those lines are for MySQL gone away error probably it will not work for your case. Actually yii2 opens one connection through the entire running time.

Please think about if you are opening new connections while running tasks. Or check server settings to see if it can not handle few connections.

Thanks,

Yeah right!
MySQL gone away is really a pain for long running script.
Now I understand your code 😄

In my case, I've a limit of 150 connection to MySQL, however my application has a multi-tenant with multi-db configuration, so every job can be related to any tenant and this require a new connection to another db that will be lazy opened only when needed by a model (first model that need a db, open the connection).

I think it's best to fix this thing in my application by closing tenant db connection.
Thanks!