IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 20056


Ignore:
Timestamp:
Oct 10, 2008, 3:16:50 PM (18 years ago)
Author:
jhoblitt
Message:

change Nebulous::Server->new() to not setup the db handle on object construction, modify Nebulous::Server->db() to be considtent with this change

Location:
trunk/Nebulous-Server
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Nebulous-Server/Changes

    r20046 r20056  
    2727    - change neb-admin to properly cleanup it's semaphore file
    2828    - retry system call failures
     29    - change Nebulous::Server->new() to not setup the db handle on object
     30      construction, modify Nebulous::Server->db() to be considtent with this
     31      change
    2932
    30330.15 Thu Sep 11 13:00:59 HST 2008
  • trunk/Nebulous-Server/lib/Nebulous/Server.pm

    r20046 r20056  
    11# Copyright (c) 2004-2008  Joshua Hoblitt
    22#
    3 # $Id: Server.pm,v 1.89 2008-10-10 21:42:59 jhoblitt Exp $
     3# $Id: Server.pm,v 1.90 2008-10-11 01:16:50 jhoblitt Exp $
    44
    55package Nebulous::Server;
     
    4545    $log->debug( "entered - @_" );
    4646
     47    my $self = bless {}, ref $class || $class;
     48    $self->log($log);
     49    $self->sql($sql);
     50    $self->config($config);
     51
     52    $log->debug( "leaving" );
     53   
     54    return $self;
     55}
     56
     57sub db {
     58    my $self = shift;
     59
     60    if (@_) {
     61        $self->{db} = $_[0];
     62        return $self;
     63    }
     64
     65    my $log     = $self->log;
     66    my $sql     = $self->sql;
     67    my $config  = $self->config;
     68
     69    # if the dbh is still alive, return it
     70    if (defined $self->{db} and $self->{db}->ping) {
     71        $log->debug("db handle is still alive");
     72        return $self->{db};
     73    }
     74    # otherwise create a new connection
     75    $log->debug("db handle is dead/unopened");
     76
     77    # if we're running under mod_perl & Apache::DBI is loaded we want to
     78    # reconnect to the database everytime the dbh is requested.  The rational is
     79    # that if we're running under mod_perl this is probably a log running
     80    # processes and the database might have gone away on us.  Apache::DBI will
     81    # take care of getting a valid dbh back.
    4782    my $db;
    4883    eval {
    49         $db = DBI->connect(
     84        $db = DBI->connect_cached(
    5085            $config->dsn,
    5186            $config->dbuser,
     
    5994
    6095        $db->do( $sql->set_transaction_model );
    61 
     96        $log->debug( "connected to database: ", sub { $db->data_sources; } );
    6297        $db->commit;
    6398        $log->debug("commit");
     
    69104    }
    70105
    71     $log->debug( "connected to database: ", sub { $db->data_sources; } );
    72 
    73     my $self = bless {}, ref $class || $class;
    74     $self->log($log);
    75     $self->sql($sql);
    76     $self->db($db);
    77     $self->config($config);
    78 
    79     $log->debug( "leaving" );
    80    
    81     return $self;
    82 }
    83 
    84 sub db {
    85     my $self = shift;
    86 
    87     if (@_) {
    88         $self->{db} = $_[0];
    89         return $self;
    90     }
    91 
    92     my $log     = $self->log;
    93     my $sql     = $self->sql;
    94     my $config  = $self->config;
    95 
    96     # if the dbh is still alive, return it
    97     if (defined $self->{db} and $self->{db}->ping) {
    98         $log->debug("db handle is still alive");
    99         return $self->{db};
    100     }
    101     # otherwise create a new connection
    102     $log->debug("db handle is dead");
    103 
    104     # if we're running under mod_perl & Apache::DBI is loaded we want to
    105     # reconnect to the database everytime the dbh is requested.  The rational is
    106     # that if we're running under mod_perl this is probably a log running
    107     # processes and the database might have gone away on us.  Apache::DBI will
    108     # take care of getting a valid dbh back.
    109     if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) {
    110         my $db;
    111         eval {
    112             $db = DBI->connect(
    113                 $config->dsn,
    114                 $config->dbuser,
    115                 $config->dbpasswd,
    116                 {
    117                     RaiseError => 1,
    118                     PrintError => 0,
    119                     AutoCommit => 0,
    120                 },
    121             );
    122 
    123             $db->do( $sql->set_transaction_model );
    124 
    125             $db->commit;
    126             $log->debug("commit");
    127         };
    128         if ( $@ ) {
    129             $db->rollback if $db;
    130             $log->debug("rollback");
    131             $log->logdie( "database error: $@" );
    132         }
    133 
    134         $self->{db} = $db;
    135 
    136         return $db;
    137     }
    138 
    139     return $self->{db};
     106    $self->{db} = $db;
     107
     108    return $db;
    140109}
    141110
     
    11811150    my $log = $self->log;
    11821151    my $sql = $self->sql;
    1183     my $db  =$self->db;
     1152    my $db  = $self->db;
    11841153
    11851154    no warnings qw( uninitialized );
Note: See TracChangeset for help on using the changeset viewer.