<!-- 
RSS generated by JIRA (5.2.7#850-sha1:b2af0c8dc8537b36121c6a579fabbdf79fc919e5) at Sat May 18 19:14:11 UTC 2013

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary add field=key&field=summary to the URL of your request.
For example:
http://www.doctrine-project.org/jira/si/jira.issueviews:issue-xml/DBAL-275/DBAL-275.xml?field=key&field=summary
-->
<rss version="0.92" >
<channel>
    <title>Doctrine Project</title>
    <link>http://www.doctrine-project.org/jira</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>5.2.7</version>
        <build-number>850</build-number>
        <build-date>21-02-2013</build-date>
    </build-info>

<item>
            <title>[DBAL-275] Automatically attempt to reconnect a dropped persistent MySQL-connection (MySQL server has gone away)</title>
                <link>http://www.doctrine-project.org/jira/browse/DBAL-275</link>
                <project id="10040" key="DBAL">Doctrine DBAL</project>
                        <description>&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;I have fixed this for DBAL 2.1.6 by adding a custom layer of Statement-, Connection- and Driver classes.&lt;br/&gt;
Key functionalities:&lt;/p&gt;
&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;The custom layer will transparently catch dropped connections and attempt a number of times (configurable) to reconnect.&lt;/li&gt;
	&lt;li&gt;The behaviour will not be triggered in a transaction (in that case it will revert to throwing an exception just like before).&lt;/li&gt;
	&lt;li&gt;The reconnect behaviour is not MySQL specific per se. It can be triggered by any Exception for any Driver-type if a Driver provides a method &apos;getReconnectExceptions&apos;.&lt;/li&gt;
	&lt;li&gt;Minimal performance-impact. Only when an Exception is thrown will it be searched for a matching Exception to possibly trigger the behaviour. (also the reason a single stringmatch is used)&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Why this functionality?&lt;/p&gt;
&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;It is often not possible to change settings of a database-server.&lt;/li&gt;
	&lt;li&gt;In a production environment the MySQL wait_timeout is often set to mere seconds&lt;/li&gt;
	&lt;li&gt;Doctrine&apos;s use of persistent connections will become a little more persistent &lt;img class=&quot;emoticon&quot; src=&quot;http://www.doctrine-project.org/jira/images/icons/emoticons/smile.gif&quot; height=&quot;20&quot; width=&quot;20&quot; align=&quot;absmiddle&quot; alt=&quot;&quot; border=&quot;0&quot;/&gt;&lt;/li&gt;
	&lt;li&gt;More reliable and robust php-scripts built on top of the DBAL&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;See files in attached archive to get an idea of the code. Enabling the layer is currently done like this (Symfony2 yml):&lt;br/&gt;
doctrine:&lt;br/&gt;
    dbal:&lt;br/&gt;
        wrapper_class: DoP\DoPBundle\Doctrine\DBAL\Connection&lt;br/&gt;
        driver_class: DoP\DoPBundle\Doctrine\DBAL\Driver\PDOMySql\Driver&lt;br/&gt;
        options:&lt;br/&gt;
            x_reconnect_attempts: 2&lt;/p&gt;

&lt;p&gt;Maybe I overlook something, but I only see pro&apos;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.&lt;/p&gt;

&lt;p&gt;If agreed I&apos;ll create a github pull request when finished with the code and take comments/improvements from there.&lt;/p&gt;

&lt;p&gt;(Also, I have glanced over &lt;a href=&quot;http://www.doctrine-project.org/contribute.html&quot; class=&quot;external-link&quot;&gt;http://www.doctrine-project.org/contribute.html&lt;/a&gt; but did not find any coding/testing-guidelines. Can you point me in the right direction?)&lt;/p&gt;

&lt;p&gt;Regards,&lt;/p&gt;

&lt;p&gt;Dieter&lt;/p&gt;</description>
                <environment>doctrine-dbal/2.1.6, driver PDOMySql</environment>
            <key id="13698">DBAL-275</key>
            <summary>Automatically attempt to reconnect a dropped persistent MySQL-connection (MySQL server has gone away)</summary>
                <type id="4" iconUrl="http://www.doctrine-project.org/jira/images/icons/issuetypes/improvement.png">Improvement</type>
                                <priority id="3" iconUrl="http://www.doctrine-project.org/jira/images/icons/priorities/major.png">Major</priority>
                    <status id="1" iconUrl="http://www.doctrine-project.org/jira/images/icons/statuses/open.png">Open</status>
                    <resolution id="-1">Unresolved</resolution>
                    <security id="10000">All</security>
                        <assignee username="beberlei">Benjamin Eberlei</assignee>
                                <reporter username="peetersdiet">Dieter Peeters</reporter>
                        <labels>
                    </labels>
                <created>Mon, 14 May 2012 16:26:49 +0000</created>
                <updated>Wed, 21 Nov 2012 11:39:50 +0000</updated>
                                    <version>2.1.6</version>
                                                        <due></due>
                    <votes>4</votes>
                        <watches>8</watches>
                        <comments>
                    <comment id="18540" author="naderman" created="Fri, 24 Aug 2012 18:42:17 +0000"  >&lt;p&gt;Sounds like a rather useful addition to me, would be cool to see this as a default feature.&lt;/p&gt;</comment>
                    <comment id="18974" author="jpauli" created="Tue, 13 Nov 2012 10:06:15 +0000"  >&lt;p&gt;We used this at work, it&apos;s simple, it could need more reflection&lt;/p&gt;</comment>
                    <comment id="18975" author="beberlei" created="Tue, 13 Nov 2012 10:12:17 +0000"  >&lt;p&gt;The problem with a generic solution here are the risks involved, we need to answer questions:&lt;/p&gt;

&lt;p&gt;1. did a transaction get aborted beccause of this&lt;br/&gt;
2. is it safe to continue at this point with a second, new connection&lt;/p&gt;

&lt;p&gt;I am not sure we can guarantee the 100% working.&lt;/p&gt;</comment>
                    <comment id="18977" author="peetersdiet" created="Tue, 13 Nov 2012 12:34:29 +0000"  >&lt;p&gt;@Julien:&lt;br/&gt;
I assume you are referring to the elaborated switch-statement? Well, since this is a DBAL I think data-integrity and performance are the more important things. In my experience when performance is important, you avoid reflection in PHP as much as possible. In the light of performance the switch-statement was a very deliberate substitution of a call to call_user_func_array. The number of arguments was finite and I am fairly sure all possible cases where covered.&lt;/p&gt;

&lt;p&gt;@Benjamin:&lt;br/&gt;
It seems I forgot to mention this in my statement above. Transactions are indeed an issue I deliberately avoided in this early version. As far as I know, transactions in MySQL are connection-based so the transaction will be rolled back when the server disconnects. A solution would be to incorporate a cache of all statements in a transaction. I was waiting on feedback of the Doctrine-developers on incorporating this code into the DBAL itself, before extending it with more functionality... and I now notice that it got assigned &lt;img class=&quot;emoticon&quot; src=&quot;http://www.doctrine-project.org/jira/images/icons/emoticons/smile.gif&quot; height=&quot;20&quot; width=&quot;20&quot; align=&quot;absmiddle&quot; alt=&quot;&quot; border=&quot;0&quot;/&gt;&lt;/p&gt;

&lt;p&gt;I&apos;ll try to answer your two questions:&lt;br/&gt;
1. If the connection is dropped when inside a transaction, the behaviour will revert to the normal Doctrine-behaviour, i.e. no reconnection-attempt will be made and the exception &apos;MySQL server has gone away&apos; will be thrown.&lt;br/&gt;
2. To be clear, I always try to write bug-free code, but I do not guarantee anything about this code. To answer your question: yes, it is safe to continue if you try-catch your transaction in your application-code. Upon the exception you reconnect to MySQL and repeat the lost transaction.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Dieter&lt;/p&gt;</comment>
                    <comment id="18978" author="beberlei" created="Tue, 13 Nov 2012 12:43:13 +0000"  >&lt;p&gt;My idea would be to throw an exception on reconnect like it is done atm, when transactionNestingLevel &amp;gt; 0, and otherwise proceed with doing the reconnect. I am not sure i am missing something here though.&lt;/p&gt;</comment>
                    <comment id="18979" author="peetersdiet" created="Tue, 13 Nov 2012 12:44:06 +0000"  >&lt;p&gt;@Benjamin: I noticed after commenting that you&apos;re the assignee ... and corrected my comment a bit.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;What do you think?&lt;/p&gt;

&lt;p&gt;*&lt;b&gt;edit&lt;/b&gt;*&lt;br/&gt;
A bit of crossposting here &lt;img class=&quot;emoticon&quot; src=&quot;http://www.doctrine-project.org/jira/images/icons/emoticons/smile.gif&quot; height=&quot;20&quot; width=&quot;20&quot; align=&quot;absmiddle&quot; alt=&quot;&quot; border=&quot;0&quot;/&gt;&lt;/p&gt;

&lt;p&gt;The way you suggest is the way this layer is implemented&lt;br/&gt;
*&lt;b&gt;edit&lt;/b&gt;*&lt;/p&gt;

&lt;p&gt;Dieter&lt;/p&gt;</comment>
                    <comment id="18980" author="peetersdiet" created="Tue, 13 Nov 2012 12:50:12 +0000"  >&lt;p&gt;@Benjamin&lt;br/&gt;
It&apos;s been a while since I wrote the code. Above answers are from memory. I will revise the code this evening and give you exact answers. Also, related to transactions, I think the way I handle the savepoints should be revised.&lt;/p&gt;</comment>
                    <comment id="18981" author="peetersdiet" created="Tue, 13 Nov 2012 13:11:59 +0000"  >&lt;p&gt;@Benjamin:&lt;br/&gt;
Sorry for the spam... &lt;img class=&quot;emoticon&quot; src=&quot;http://www.doctrine-project.org/jira/images/icons/emoticons/smile.gif&quot; height=&quot;20&quot; width=&quot;20&quot; align=&quot;absmiddle&quot; alt=&quot;&quot; border=&quot;0&quot;/&gt;&lt;/p&gt;

&lt;p&gt;I couldn&apos;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 &amp;lt; 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.&lt;/p&gt;</comment>
                    <comment id="19016" author="pkruithof" created="Wed, 21 Nov 2012 11:11:16 +0000"  >&lt;p&gt;I could really use this for kong running cronjobs and daemonized scripts. Is this being worked on right now?&lt;/p&gt;</comment>
                    <comment id="19017" author="peetersdiet" created="Wed, 21 Nov 2012 11:39:50 +0000"  >&lt;p&gt;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.&lt;/p&gt;</comment>
                </comments>
                    <attachments>
                    <attachment id="11348" name="doctrine-connection.php" size="4948" author="jpauli" created="Tue, 13 Nov 2012 10:06:15 +0000" />
                    <attachment id="11189" name="reconnect_layer.tar.gz" size="2003" author="peetersdiet" created="Mon, 14 May 2012 16:30:01 +0000" />
                </attachments>
            <subtasks>
        </subtasks>
        </item>
</channel>
</rss>