[Koha-patches] [PATCH] Database connectivity cleanups

Clay Fouts clay.fouts at liblime.com
Thu Oct 30 03:22:15 CET 2008


This patch employs process-local caching of systempreferences and
eliminates the "select 1" execution every time dbh() is called.
DBI::ping() is used instead.
---
 C4/Context.pm |   22 ++++++++++++++--------
 1 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/C4/Context.pm b/C4/Context.pm
index 7813f49..e3be714 100644
--- a/C4/Context.pm
+++ b/C4/Context.pm
@@ -461,12 +461,19 @@ with this method.
 
 =cut
 
-# FIXME - The preferences aren't likely to change over the lifetime of
-# the script (and things might break if they did change), so perhaps
-# this function should cache the results it finds.
+# FIXME: running this under mod_perl will require a means of
+# flushing the caching mechanism.
+
+my %sysprefs;
+
 sub preference {
     my $self = shift;
     my $var  = shift;                          # The system preference to return
+
+    if (exists $sysprefs{$var}) {
+        return $sysprefs{$var};
+    }
+
     my $dbh  = C4::Context->dbh or return 0;
 
     # Look up systempreferences.variable==$var
@@ -476,8 +483,8 @@ sub preference {
         WHERE    variable=?
         LIMIT    1
 END_SQL
-    my $retval = $dbh->selectrow_array( $sql, {}, $var );
-    return $retval;
+    $sysprefs{$var} = $dbh->selectrow_array( $sql, {}, $var );
+    return $sysprefs{$var};
 }
 
 sub boolean_preference ($) {
@@ -681,9 +688,8 @@ sub dbh
     my $self = shift;
     my $sth;
 
-    if (defined($context->{"dbh"})) {
-        $sth=$context->{"dbh"}->prepare("select 1");
-        return $context->{"dbh"} if (defined($sth->execute));
+    if (defined($context->{"dbh"}) && $context->{"dbh"}->ping()) {
+	return $context->{"dbh"};
     }
 
     # No database handle or it died . Create one.
-- 
1.5.6.5




More information about the Koha-patches mailing list