summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Haas2009-05-24 02:00:19 +0000
committerRobert Haas2009-05-24 02:00:19 +0000
commitc393c68db3df467ee02dbc83aad4662e36cc7629 (patch)
tree4ec308daa9e4846e017d706960d16bfa2c73b351
parenta1bb7bcda0069888fb580d0b7089bd9e3634de06 (diff)
Avoid opening the database connection sooner than necessary.
-rw-r--r--perl-lib/PgCommitFest/Handler.pm10
-rw-r--r--perl-lib/PgCommitFest/Request.pm14
2 files changed, 15 insertions, 9 deletions
diff --git a/perl-lib/PgCommitFest/Handler.pm b/perl-lib/PgCommitFest/Handler.pm
index 13af0fd..6ad00d9 100644
--- a/perl-lib/PgCommitFest/Handler.pm
+++ b/perl-lib/PgCommitFest/Handler.pm
@@ -27,16 +27,11 @@ our %ACTION = (
'patch_comment_delete' => \&PgCommitFest::PatchComment::delete
);
-our $PG = 'dbi:Pg:dbname=pgcommitfest';
-our $PGUSERNAME = 'pgcommitfest';
-our $PGPASSWORD = '';
-
sub main_loop {
$SIG{'PIPE'} = sub { die "SIGPIPE\n"; };
while (1) {
# Invoke main request handler and save any resulting error message.
- my $db = PgCommitFest::DB->connect($PG, $PGUSERNAME, $PGPASSWORD);
- my $r = PgCommitFest::Request->new($db);
+ my $r = PgCommitFest::Request->new();
last if !defined $r;
eval {
handler($r);
@@ -47,7 +42,7 @@ sub main_loop {
my $err = $@;
# Roll back any uncommited database work.
- $db->tidy;
+ $r->db->tidy if $r->db_is_connected;
# Print errors to system log.
if ($err && $err ne "SIGPIPE\n" && $err ne "DONE\n") {
@@ -57,7 +52,6 @@ sub main_loop {
$r->render_template('error', { 'error_list' => [ $err ] });
}
}
- $db->disconnect;
}
}
diff --git a/perl-lib/PgCommitFest/Request.pm b/perl-lib/PgCommitFest/Request.pm
index 4ceaf7f..ae38399 100644
--- a/perl-lib/PgCommitFest/Request.pm
+++ b/perl-lib/PgCommitFest/Request.pm
@@ -12,6 +12,9 @@ die "Must set PGCOMMITFEST_ROOT to root directory of installation!\n"
our $ROOT = $ENV{'PGCOMMITFEST_ROOT'};
our $template = Template->new({ 'INCLUDE_PATH' => $ROOT . '/template',
'FILTERS' => { 'htmlsafe' => \&PgCommitFest::WebControl::escape } });
+our $PG = 'dbi:Pg:dbname=pgcommitfest';
+our $PGUSERNAME = 'pgcommitfest';
+our $PGPASSWORD = '';
$CGI::POST_MAX = 65536;
$CGI::DISABLE_UPLOADS = 1; # No need for uploads at present.
@@ -23,7 +26,7 @@ sub new {
'cgi' => $cgi,
'control' => {},
'control_list' => [],
- 'db' => $db,
+ 'db' => undef,
'error_list' => [],
'header' => {
'Content-type' => 'text/html',
@@ -105,9 +108,18 @@ sub control {
sub db {
my ($self) = @_;
+ if (!defined $self->{'db'}) {
+ $self->{'db'} =
+ PgCommitFest::DB->connect($PG, $PGUSERNAME, $PGPASSWORD);
+ }
return $self->{'db'};
}
+sub db_is_connected {
+ my ($self) = @_;
+ defined $self->{'db'};
+}
+
sub error {
my ($self, $fmt, @arg) = @_;
push @{$self->{'error_list'}}, sprintf($fmt, @arg);