Index: trunk/Nebulous/lib/Nebulous/Server.pm
===================================================================
--- trunk/Nebulous/lib/Nebulous/Server.pm	(revision 10546)
+++ trunk/Nebulous/lib/Nebulous/Server.pm	(revision 10575)
@@ -1,5 +1,5 @@
 # Copyright (c) 2004  Joshua Hoblitt
 #
-# $Id: Server.pm,v 1.20 2006-12-08 03:29:19 jhoblitt Exp $
+# $Id: Server.pm,v 1.21 2006-12-08 20:57:36 jhoblitt Exp $
 
 package Nebulous::Server;
@@ -20,5 +20,5 @@
 use Params::Validate qw( validate_pos SCALAR );
 
-__PACKAGE__->mk_accessors(qw( log sql db ));
+__PACKAGE__->mk_accessors(qw( log sql config ));
 
 sub new {
@@ -66,6 +66,55 @@
     $self->sql($sql);
     $self->db($db);
+    $self->config($config);
 
     return $self;
+}
+
+sub db {
+    my $self = shift;
+
+    if (@_) {
+        $self->{db} = $_[0];
+        return $self;
+    }
+    
+    my $log     = $self->log;
+    my $sql     = $self->sql;
+    my $config  = $self->config;
+
+    # if we're running under mod_perl & Apache::DBI is loaded we want to
+    # reconnect to the database everytime the dbh is request.  The rational is
+    # that if we're running under mod_perl this is probably a log running
+    # processes and the database might have gone away on us.  Apache::DBI will
+    # take care of getting a valid dbh back.
+    if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) {
+        my $db;
+        eval {
+            $db = DBI->connect(
+                $config->dsn,
+                $config->dbuser,
+                $config->dbpasswd,
+                {
+                    RaiseError => 1,
+                    PrintError => 0,
+                    AutoCommit => 0,
+                },
+            );
+
+            $db->do( $sql->set_transaction_model );
+
+            $db->commit;
+        };
+        if ( $@ ) {
+            $db->rollback if $db;
+            $log->logdie( "database error: $@" );
+        }
+
+        $self->{db} = $db;
+
+        return $db;
+    }
+
+    return $self->{db};
 }
 
