-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DBAL-275: Automatically attempt to reconnect a dropped persistent MySQL-connection (MySQL server has gone away) #1454
Comments
Comment created by naderman: Sounds like a rather useful addition to me, would be cool to see this as a default feature. |
Comment created by jpauli: We used this at work, it's simple, it could need more reflection |
Comment created by @beberlei: The problem with a generic solution here are the risks involved, we need to answer questions:
I am not sure we can guarantee the 100% working. |
Comment created by peetersdiet: @julien: @benjamin: I'll try to answer your two questions:
And as a last note, this code is just to have a workable solution. I tend to agree with anyone who thinks that the correct place to fix this problem is in the MySQL-client itself. Dieter |
Comment created by @beberlei: My idea would be to throw an exception on reconnect like it is done atm, when transactionNestingLevel > 0, and otherwise proceed with doing the reconnect. I am not sure i am missing something here though. |
Comment created by peetersdiet: @benjamin: I noticed after commenting that you're the assignee ... and corrected my comment a bit. Now, if you want I can extend the functionality to support transactions. But I prefer to do this directly into the DBAL, not as a layer on top. The resulting code should be a bit cleaner than this layer now. What do you think? _edit_ The way you suggest is the way this layer is implemented Dieter |
Comment created by peetersdiet: @benjamin |
Comment created by peetersdiet: @benjamin: I couldn't help myself doing a quick verification. The answer to your question lies in the file Connection.php. The method DoP\DoPBundle\Doctrine\DBAL\Connection::validateReconnectAttempt also checks that the transactionNestingLevel < 1, so the method will always return false when in a transaction. I.o.w. when in a transaction no attempt to reconnect will be made and the exception is simply rethrown, as per the default Doctrine behaviour. |
Comment created by pkruithof: I could really use this for kong running cronjobs and daemonized scripts. Is this being worked on right now? |
Comment created by peetersdiet: Peter, you can use the above code, but it only works for statements outside of transactions. Also, the calls for savepoints in transactions need correction. Will do that when I find the time. |
Comment created by saeven: This was exactly what I needed...almost! Minor tweaks where it didn't work with current version of Doctrine on ZF2 (iterators req., and mismatched signatures). It also looked for 2006s at the head of the error message, when they can appear in the middle. Lastly, our darned modem was partly to blame, dropping on DNS lookups resulting in network errors. So I'll say big thanks Dieter, and I've posted a small set of tweaks to your classes at http://circlical.com/blog/2013/9/12/mysql-server-has-gone-away-atop-doctrine2-and-zend-framework-2. If this helps another soul, glad to give back; and, thanks again. |
Issue was closed with resolution "Fixed" |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Jira issue originally created by user peetersdiet:
For php-scripts that run for a long time (a.o. daemons) persistent connections will almost always be dropped by the MySQL-server after a set timeout (depending on wait_timeout). This will have Doctrine throw an exception and have the php-script terminate if not catched. It is not practical to catch the same Exception with a try-catch around every query.
I have fixed this for DBAL 2.1.6 by adding a custom layer of Statement-, Connection- and Driver classes.
Key functionalities:
Why this functionality?
See files in attached archive to get an idea of the code. Enabling the layer is currently done like this (Symfony2 yml):
doctrine:
dbal:
wrapper_class: DoP\DoPBundle\Doctrine\DBAL\Connection
driver_class: DoP\DoPBundle\Doctrine\DBAL\Driver\PDOMySql\Driver
options:
x_reconnect_attempts: 2
Maybe I overlook something, but I only see pro's, no cons, to this improvement. I have created this issue to poll if you think this is a welcome feature and are interested to have me rework the code into DBAL itself? Reworking it into DBAL itself would certainly greatly reduce my code.
If agreed I'll create a github pull request when finished with the code and take comments/improvements from there.
(Also, I have glanced over http://www.doctrine-project.org/contribute.html but did not find any coding/testing-guidelines. Can you point me in the right direction?)
Regards,
Dieter
The text was updated successfully, but these errors were encountered: