Friday 15 July 2011

MySQL "Gone Away" Error with Persistent PHP Connection -



MySQL "Gone Away" Error with Persistent PHP Connection -

i'm hosting website locally on wamp stack. switched php connection persistent adding array(pdo::attr_persistent => true) pdo constructor options argument. i've noticed material drop in response time result (hooray!).

the downside seems gone away error when machine wakes up. never happened before changing connection style.

is possible cached connection closed, continues returned? possible reset pdo connection or reset connection pool via php within grab block?

i've kicked around few days , based on prevalence of similar issues on web, appears deficiency of pdo preventing efficient managing of persistent connections.

answers obvious questions:

php 5.4.22 driver settings in php.ini have persistent connections turned on session limits not bounded (set -1) pool limits not bounded (set -1)

i can recreate issue doing following:

issue next statements on mysql database.

set @@global.interactive_timeout := 10; set @@global.wait_timeout := 10;

issue few requests against server generate cached connections. can see thread count increment compared doing non-persistent connections via:

echo $conn->getattribute(pdo::attr_server_info);

wait @ to the lowest degree 10 seconds , start issuing more requests. should start receiving 'gone away' messages.

the issue sql closes connections , subsequent calls pdo constructor homecoming these closed connections without reconnecting them.

this pdo deficient. there no way forcefulness connection open , no way observe state.

the way i'm getting around (admittedly bit of hack) issuing these mysql statements

set @@global.interactive_timeout := 86400; set @@global.wait_timeout := 86400;

these variables set 28800sec (8 hours) default. note you'll want restart apache clear out cached connections or wont notice difference until connections in pool have been cycled (i have no thought how / when happens). chose 86400 24 hours , i'm on machine daily should cover basic need.

after update allow machine sit down @ to the lowest degree 12 hours how long sat when started getting 'gone away message'. looks problem solved.

i've been thinking while cant forcefulness open connection, may possible remove bad connection pool. haven't tried this, more elegant solution might observe 'gone away' message set object null telling php destroy resource. if database logic made few attempts (there'd have limit in case more severe error occurred), might help maintain these errors minimum.

php mysql pdo

No comments:

Post a Comment