Problem with persistance of connection under mod_perl

[prev] [thread] [next] [Date index for 2004/06/11]

From: Andreas Fromm
Subject: Problem with persistance of connection under mod_perl
Date: 09:19 on 11 Jun 2004
I'm trying to build a WebApp for a databse using CDBI and CGI::Builder,
but am haveing some problems with the persistance of the DB conncetion
between tow invocations of the script. I need the username and password to
perform a per user login to the DB, which I use as authentication method
to the App. The dbh created by the login-subroutine should then be the
dbh for the CDBI session. Thatfor I'm inheriting my base class fromm
CDBI::BaseDSN and loading the table classes after by a require when
they are needed by the Pagehandlers of the App, i.e after the login process.

Here is waht I'M; doinmg right now.

WebKRaM.cgi:
use WebKraM;

$|=1;

my $webapp = WebKraM->new() ;
$webapp->process() ;
------------

WebKraM.pm:
package WebKraM;

use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

use CGI::Builder
  qw| CGI::Builder::Magic
      CGI::Builder::Session
      CGI::Builder::Test
    |;

use base 'DbKraM';

sub OH_pre_process
  {
    my $self = shift;
    $self->cs->param('logged_in') || $self->switch_to('login');
  }

sub SH_login
  {
    my $self = shift;
    my $user = $self->cgi->param("user");
    my $passw= $self->cgi->param("passw");

    my $host = "localhost";
    my $database = "KraM";

    my $dsn = "DBI:Pg:dbname=$database;host=$host";

    eval {
      $self->set_db( Main => $dsn, $user, $passw );
    };
    my $p = $self->cgi->param('p');

    if ( DbKraM->login($user, $passw) ) {
      $self->cs->param("logged_in", 1);
      if ( $p and ($p ne 'login') ){
	return $self->switch_to($p);
      } else {
	return $self->switch_to('main');
      }
    }

  }

sub PH_data_table {
  require DbKraM::data_table;

  my $self = shift;

  my $obj =
    DbKraM::data_table->retrieve( $self->cgi->param("$who.id") );

  #  ....do something usefull with $obj
}

1;
------------

DbKraM.pm:
package DbKraM;

use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

use base 'Class::DBI::BaseDSN';

sub login{
  my $self = shift;

  my ($user, $passw) = @_;

  my $host = "localhost";
  my $database = "KraM";

  my $dsn = "DBI:Pg:dbname=$database;host=$host";

  my $dbh;

  eval {
    $self->set_db( Main => $dsn, $user, $passw );
  };
  eval {
    $dbh = DbKraM->db_Main;
  };

  return $@ ? 0 : 1;
}

1;
------------

DbKraM/data_table.pm
package DbKraM::eltern;
use DbKraM;

__PACKAGE__->set_up_table('eltern');

1;
------------


The problem arise when the package DbKraM::data_table is required. The
script breaks with the message like:

Fatal error in phase PRE_PROCESS for page "eltern": DBI
connect('dbname=KraM;host=localhost','',...) failed: FATAL:
user "www-data" does not exist at /usr/share/perl5/Ima/DBI.pm


Any suggestions?


---
regards,

Andreas Fromm


Problem with persistance of connection under mod_perl
Andreas Fromm 09:19 on 11 Jun 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52