From 55e97fc278cc7f26127f54204798aae89a7c66c7 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 29 Mar 2016 13:43:58 +0100 Subject: [PATCH] Bug 16156: Do not ensure the dbh is connected all the time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I have profiled `/catalogue/search.pl?q=d` (20 results, xslt) and found that at least 600ms are spent in DBIx::Class::Storage::DBI::ensure_connected, which is called 5749 times (almost 1 per query). Ideally we could work on the basis that the connection is only checked when a thread is started. This is what does this patch. Unfortunately this patch will revive timeout issues with long run process (SIP server for instance). Before this patch: 901398 statements and 346866 subroutine calls in 406 source files and 55 string evals spent 637ms (33.0+604) within DBIx::Class::Storage::DBI::ensure_connected which was called 5749 times, avg 111µs/call After this patch: 817161 statements and 265940 subroutine calls in 406 source files and 55 string evals spent 49.8ms (6.58+43.2) within DBIx::Class::Storage::DBI::ensure_connected which was called 343 times, avg 145µs/call --- C4/Context.pm | 16 +++++++++++++--- debian/templates/plack.psgi | 2 ++ misc/plack/koha.psgi | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index 6b70b60..ac46471 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -775,7 +775,8 @@ sub _new_Zconn { sub _new_dbh { - Koha::Database->schema({ new => 1 })->storage->dbh; + $context->{dbh} = Koha::Database->schema({ new => 1 })->storage->dbh; + return $context->{dbh}; } =head2 dbh @@ -801,10 +802,13 @@ sub dbh my $sth; unless ( $params->{new} ) { - return Koha::Database->schema->storage->dbh; + return $context->{dbh} if $context->{dbh}; + $context->{dbh} = Koha::Database->schema->storage->dbh; + return $context->{dbh}; } - return Koha::Database->schema({ new => 1 })->storage->dbh; + $context->{dbh} = Koha::Database->schema({ new => 1 })->storage->dbh; + return $context->{dbh}; } =head2 new_dbh @@ -1182,6 +1186,12 @@ sub interface { return $context->{interface} // 'opac'; } +sub flush { + my ( $class ) = @_; + delete $context->{dbh} + if $context and $context->{dbh}; +} + 1; __END__ diff --git a/debian/templates/plack.psgi b/debian/templates/plack.psgi index 995fa72..13873a6 100644 --- a/debian/templates/plack.psgi +++ b/debian/templates/plack.psgi @@ -26,6 +26,7 @@ use Plack::App::Directory; use Plack::App::URLMap; # Pre-load libraries +use C4::Context; use C4::Boolean; use C4::Branch; use C4::Category; @@ -45,6 +46,7 @@ use CGI qw(-utf8 ); # we will loose -utf8 under plack, otherwise my $q = $old_new->( @_ ); $CGI::PARAM_UTF8 = 1; Koha::Cache->flush_L1_cache(); + C4::Context->flush(); return $q; }; } diff --git a/misc/plack/koha.psgi b/misc/plack/koha.psgi index 7f3ea27..b551cda 100644 --- a/misc/plack/koha.psgi +++ b/misc/plack/koha.psgi @@ -13,6 +13,7 @@ use CGI qw(-utf8 ); # we will lose -utf8 under plack my $q = $old_new->( @_ ); $CGI::PARAM_UTF8 = 1; Koha::Cache->flush_L1_cache(); + C4::Context->flush(); return $q; }; } -- 2.7.0