diff options
| author | Russ Allbery <rra@stanford.edu> | 2007-08-29 16:50:15 +0000 | 
|---|---|---|
| committer | Russ Allbery <rra@stanford.edu> | 2007-08-29 16:50:15 +0000 | 
| commit | 44f55bbbb6e62588ed45092abed74efff97ae539 (patch) | |
| tree | d3c5c8176164f44aa87006391259e25b68a170ea | |
| parent | 9d0b4df45a4be981b8d23e9cd37e70298e3dec59 (diff) | |
Wallet::Server is now responsible for connecting to the database.  Don't
bother working hard to disconnect the database on exit, since the DESTROY
DBI method will do the right thing.
| -rw-r--r-- | perl/Wallet/Server.pm | 30 | ||||
| -rwxr-xr-x | server/wallet-backend | 21 | 
2 files changed, 25 insertions, 26 deletions
| diff --git a/perl/Wallet/Server.pm b/perl/Wallet/Server.pm index 6a95e06..902e86d 100644 --- a/perl/Wallet/Server.pm +++ b/perl/Wallet/Server.pm @@ -36,14 +36,32 @@ $VERSION = '0.01';  ##############################################################################  # Create a new wallet server object.  A new server should be created for each -# user who is making changes to the wallet.  Takes the database handle that -# will be used for all of the wallet metadata and the principal and host who -# are sending wallet requests.  We also instantiate the administrative ACL, -# which we'll use for various things.  Throw an exception if anything goes -# wrong. +# user who is making changes to the wallet.  Takes the principal and host who +# are sending wallet requests.  Opens a connection to the database that will +# be used for all of the wallet metadata based on the wallet configuration +# information.  We also instantiate the administrative ACL, which we'll use +# for various things.  Throw an exception if anything goes wrong.  sub new { -    my ($class, $dbh, $user, $host) = @_; +    my ($class, $user, $host) = @_;      my $acl = Wallet::ACL->new ('ADMIN'); +    unless ($DB_DRIVER and ($DB_INFO or $DB_NAME)) { +        die "database connection information not configured\n"; +    } +    my $dsn = "DBI:$DB_DRIVER:"; +    if ($DB_INFO) { +        $dsn .= $DB_INFO; +    } else { +        $dsn .= "database=$DB_NAME"; +        $dsn .= ";host=$DB_HOST" if $DB_HOST; +        $dsn .= ";port=$DB_PORT" if $DB_PORT; +    } +    my $dbh = DBI->connect ($dsn, $DB_USER, $DB_PASSWORD); +    if (not defined $dbh) { +        die "cannot connect to database: $DBI::errstr\n"; +    } +    $dbh->{AutoCommit} = 0; +    $dbh->{RaiseError} = 1; +    $dbh->{PrintError} = 0;      my $self = {          dbh   => $dbh,          user  => $user, diff --git a/server/wallet-backend b/server/wallet-backend index 24910cd..e8617cd 100755 --- a/server/wallet-backend +++ b/server/wallet-backend @@ -20,21 +20,6 @@ use Wallet::Config;  use Wallet::Server;  ############################################################################## -# Database handling -############################################################################## - -# Open a new database connection.  This is a separate function to make it -# easier to override later. -sub db_connect { -    my $dsn = "DBI:$DB_DRIVER:database=$DB_NAME;host=$DB_HOST;port=$DB_PORT"; -    my $dbh = DBI->connect ($dsn, $DB_USER, $DB_PASSWORD); -    if (not defined $dbh) { -        die "Cannot connect to database: $DBI::errstr\n"; -    } -    return $dbh; -} - -##############################################################################  # Implementation  ############################################################################## @@ -47,12 +32,8 @@ my $user = $ENV{REMOTE_USER} or die "REMOTE_USER not set\n";  my $host = $ENV{REMOTE_HOST} || $ENV{REMOTE_ADDR}      or die "Neither REMOTE_HOST nor REMOTE_USER set\n"; -# Open the database connection and close it cleanly on exit. -my $dbh = db_connect; -END { $dbh->disconnect; } -  # Instantiate the server object. -my $server = Wallet::Server->new ($dbh, $user, $host); +my $server = Wallet::Server->new ($user, $host);  # Parse command-line options and dispatch to the appropriate calls.  my ($command, @args) = @ARGV; | 
