Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: DBI Question

by radiantmatrix (Parson)
on Jun 16, 2008 at 17:44 UTC ( #692332=note: print w/ replies, xml ) Need Help??


in reply to DBI Question

MySQL server has gone away means that you're no longer connected to the server. Either you have disconnected (e.g. you've let your database handle go out of scope) or your server is dropping the connection on its end.

It's a problem you'll have to address by checking to see if the connection is alive and re-connecting as needed. One way to do this is using DBIx::Abstract:

use DBIx::Abstract; my $dbx = DBIx::Abstract->connect(...); sub do_query { $dbx->ensure_connection; ## do the query; }
If you're not going to use DBIx::Abstract, the same basic idea can be had from simple DBI:
$dbh = DBI->connect(...); sub do_query { my $connect; eval { ($connect) = $dbh->selectrow_array('SELECT 1'); }; if ($@ || !$connect) { $dbh = DBI->connect(...) or die "Can't reconnect: $DBI::err"; + } ## do query. }

I do recommend DBIx::Abstract, though -- you can still get at the database handle from it, but it makes moving from MySQL to PostgresQL or Oracle a snap!

<radiant.matrix>
Ramblings and references
“A positive attitude may not solve all your problems, but it will annoy enough people to make it worth the effort.” Herm Albright
I haven't found a problem yet that can't be solved by a well-placed trebuchet


Comment on Re: DBI Question
Select or Download Code
Re^2: DBI Question
by kings (Sexton) on Jun 17, 2008 at 03:01 UTC
    Another option: See the documentation for DBD::mysql and search for 'reconnect'.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://692332]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2014-12-27 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls