From 8ba2d987a2eb02c50e6a0df6463c0008af21e81d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 16 Jul 2013 11:53:47 +0200 Subject: [PATCH] Bug 10611: Use mysql_auto_reconnect instead of ping DBD::Mysql provides a mysql_auto_reconnect flag. Using it avoid to do a ping. Benchmarks: use Modern::Perl; use C4::Context; for ( 1 .. 1000 ) { $dbh = C4::Context->dbh; } * without this patch on a local DB: perl t.pl 0,49s user 0,02s system 98% cpu 0,525 total * without this patch on a remote DB: perl t.pl 0,52s user 0,05s system 1% cpu 37,358 total * with this patch on a local DB: perl t.pl 0,46s user 0,04s system 99% cpu 0,509 total * with this patch on a remote DB: perl t.pl 0,49s user 0,02s system 56% cpu 0,892 total Testing the auto reconnect: use Modern::Perl; use C4::Context; my $ping = $dbh->ping; say $ping; $dbh->disconnect; $ping = $dbh->ping; say $ping; --- C4/Context.pm | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index 7709f7e..837bbaf 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -782,12 +782,12 @@ sub _new_Zconn { # Internal helper function (not a method!). This creates a new # database connection from the data given in the current context, and # returns it. +our $db_driver; sub _new_dbh { ## $context ## correct name for db_schme - my $db_driver; if ($context->config("db_scheme")){ $db_driver=db_scheme2dbi($context->config("db_scheme")); }else{ @@ -817,6 +817,10 @@ sub _new_dbh $dbh->{RaiseError} = 0; } + if ( $db_driver eq 'mysql' ) { + $dbh->{mysql_auto_reconnect} = 1; + } + my $tz = $ENV{TZ}; if ( $db_driver eq 'mysql' ) { # Koha 3.0 is utf-8, so force utf8 communication between mySQL and koha, whatever the mysql default config. @@ -853,8 +857,10 @@ sub dbh my $self = shift; my $sth; - if (defined($context->{"dbh"}) && $context->{"dbh"}->ping()) { - return $context->{"dbh"}; + if ( defined $db_driver && $db_driver eq 'mysql' && $context->{"dbh"} ) { + return $context->{"dbh"}; + } elsif ( defined $db_driver && defined($context->{"dbh"}) && $context->{"dbh"}->ping()) { + return $context->{"dbh"}; } # No database handle or it died . Create one. -- 1.7.10.4