Database connectivity cleanups
authorClay Fouts <clay.fouts@liblime.com>
Thu, 30 Oct 2008 02:22:15 +0000 (19:22 -0700)
committerGalen Charlton <galen.charlton@liblime.com>
Thu, 6 Nov 2008 21:53:25 +0000 (15:53 -0600)
This patch employs process-local caching of systempreferences and
eliminates the "select 1" execution every time dbh() is called.
DBI::ping() is used instead.

Signed-off-by: Galen Charlton <galen.charlton@liblime.com>
C4/Context.pm

index 7813f49..e3be714 100644 (file)
@@ -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.