Skip to content

Commit

Permalink
Make the library browser database connections use a cached connection
Browse files Browse the repository at this point in the history
Currently the database connections used by the library browser are
obtained with a `DBI->connect` call, and those connections are never
closed.  As a result each time the library browser is accessed, a new
database connection is obtained, and the previous connections are left
open until the server process that created the connection is recycled.
This results in the database connection count being unnecessarily
incremented.

This changes those calls to `DBI->connect_cached`.  This means that if
the process already has a database connection, it will be reused instead
of a new one being made.  Thus keeping the database connection count
down.

See https://webwork.maa.org/moodle/mod/forum/discuss.php?d=8355 for the
discussion that led to this pull request.
  • Loading branch information
drgrice1 committed Sep 8, 2023
1 parent 8d0df7b commit a961b45
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 40 deletions.
53 changes: 17 additions & 36 deletions lib/WeBWorK/Utils/LibraryStats.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,43 @@
# Artistic License for more details.
################################################################################

###########################
# Utils::LibraryLocalStats
#
# This is an interface for getting local statistics about library problems
# for display
###########################

# This is an interface for getting global and local statistics about library problems for display.
package WeBWorK::Utils::LibraryStats;

use base qw(Exporter);
use strict;
use warnings;
use DBI;

our @EXPORT = ();
our @EXPORT_OK = qw();
use DBI;

sub new {
my $class = shift;
my $ce = shift;
my ($class, $ce) = @_;

my $dbh = DBI->connect(
$ce->{problemLibrary_db}->{dbsource},
$ce->{problemLibrary_db}->{user},
$ce->{problemLibrary_db}->{passwd},
my $dbh = DBI->connect_cached(
$ce->{problemLibrary_db}{dbsource},
$ce->{problemLibrary_db}{user},
$ce->{problemLibrary_db}{passwd},
{
PrintError => 0,
RaiseError => 0,
},
);

my $localselectstm = $dbh->prepare("SELECT * FROM OPL_local_statistics WHERE source_file = ?");

my $globalselectstm = $dbh->prepare("SELECT * FROM OPL_global_statistics WHERE source_file = ?");

my $self = {
return bless {
dbh => $dbh,
localselectstm => $localselectstm,
globalselectstm => $globalselectstm,
};

bless($self, $class);
return $self;
localselectstm => $dbh->prepare("SELECT * FROM OPL_local_statistics WHERE source_file = ?"),
globalselectstm => $dbh->prepare("SELECT * FROM OPL_global_statistics WHERE source_file = ?"),
}, $class;
}

sub getLocalStats {
my $self = shift;
my $source_file = shift;
my ($self, $source_file) = @_;

my $selectstm = $self->{localselectstm};

unless ($selectstm->execute($source_file)) {
if ($selectstm->errstr =~ /Table .* doesn't exist/) {
warn
"Couldn't find the OPL local statistics table. Did you download the latest OPL and run update-OPL-statistics.pl?";
warn "Couldn't find the OPL local statistics table. "
. "Did you download the latest OPL and run update-OPL-statistics.pl?";
}
die $selectstm->errstr;
}
Expand All @@ -87,15 +69,14 @@ sub getLocalStats {
}

sub getGlobalStats {
my $self = shift;
my $source_file = shift;
my ($self, $source_file) = @_;

my $selectstm = $self->{globalselectstm};

unless ($selectstm->execute($source_file)) {
if ($selectstm->errstr =~ /Table .* doesn't exist/) {
warn
"Couldn't find the OPL global statistics table. Did you download the latest OPL and run load-OPL-global-statistics.pl?";
warn "Couldn't find the OPL global statistics table. "
. "Did you download the latest OPL and run load-OPL-global-statistics.pl?";
}
die $selectstm->errstr;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/WeBWorK/Utils/ListingDB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ sub getTables {

sub getDB {
my $ce = shift;
my $dbh = DBI->connect(
$ce->{problemLibrary_db}->{dbsource},
$ce->{problemLibrary_db}->{user},
$ce->{problemLibrary_db}->{passwd},
my $dbh = DBI->connect_cached(
$ce->{problemLibrary_db}{dbsource},
$ce->{problemLibrary_db}{user},
$ce->{problemLibrary_db}{passwd},
{
PrintError => 0,
RaiseError => 1,
Expand Down

0 comments on commit a961b45

Please sign in to comment.