IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 10575 for trunk/Nebulous/lib


Ignore:
Timestamp:
Dec 8, 2006, 10:57:51 AM (20 years ago)
Author:
jhoblitt
Message:

add a Nebulous::Server->config() accessor
make Nebulous::Server->db() Apache::DBI aware

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Nebulous/lib/Nebulous/Server.pm

    r10546 r10575  
    11# Copyright (c) 2004  Joshua Hoblitt
    22#
    3 # $Id: Server.pm,v 1.20 2006-12-08 03:29:19 jhoblitt Exp $
     3# $Id: Server.pm,v 1.21 2006-12-08 20:57:36 jhoblitt Exp $
    44
    55package Nebulous::Server;
     
    2020use Params::Validate qw( validate_pos SCALAR );
    2121
    22 __PACKAGE__->mk_accessors(qw( log sql db ));
     22__PACKAGE__->mk_accessors(qw( log sql config ));
    2323
    2424sub new {
     
    6666    $self->sql($sql);
    6767    $self->db($db);
     68    $self->config($config);
    6869
    6970    return $self;
     71}
     72
     73sub db {
     74    my $self = shift;
     75
     76    if (@_) {
     77        $self->{db} = $_[0];
     78        return $self;
     79    }
     80   
     81    my $log     = $self->log;
     82    my $sql     = $self->sql;
     83    my $config  = $self->config;
     84
     85    # if we're running under mod_perl & Apache::DBI is loaded we want to
     86    # reconnect to the database everytime the dbh is request.  The rational is
     87    # that if we're running under mod_perl this is probably a log running
     88    # processes and the database might have gone away on us.  Apache::DBI will
     89    # take care of getting a valid dbh back.
     90    if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) {
     91        my $db;
     92        eval {
     93            $db = DBI->connect(
     94                $config->dsn,
     95                $config->dbuser,
     96                $config->dbpasswd,
     97                {
     98                    RaiseError => 1,
     99                    PrintError => 0,
     100                    AutoCommit => 0,
     101                },
     102            );
     103
     104            $db->do( $sql->set_transaction_model );
     105
     106            $db->commit;
     107        };
     108        if ( $@ ) {
     109            $db->rollback if $db;
     110            $log->logdie( "database error: $@" );
     111        }
     112
     113        $self->{db} = $db;
     114
     115        return $db;
     116    }
     117
     118    return $self->{db};
    70119}
    71120
Note: See TracChangeset for help on using the changeset viewer.