geektron has asked for the wisdom of the Perl Monks concerning the following question:

OK -- so i have mod_perl set up, created a startup.pl that looks something like this:
#!/usr/local/bin/perl use strict; use lib qw( /webcontent/server_home/rentsavers/test ); $ENV{MOD_PERL} or die "not running mod_perl!: $! \n"; use Apache::Registry; use Apache::DBI (); use DBI (); # Initialize the database connections for each child Apache::DBI->connect_on_init ("DBI:mysql:database=test;host=localhost", "testuser", { PrintError => 1, # warn() on errors RaiseError => 0, # don't die on error AutoCommit => 1, # commit executes immediately } ); 1;
from reading the perldoc, the online guide, and Writing Apache Modules w/ Perl and C ... i can use DBI normally in a handler, and the caching, etc is transparent. but i'm not seeing anything DBI-related with this handler:
package My::Greeting; use strict; use Apache::Constants qw(OK); use Data::Dumper; use DBI; sub handler { my $r = shift; my $now = scalar localtime; my $server_name = $r->server->server_hostname; $r->send_http_header('text/plain'); print <<EOT; Thanks for visiting $server_name. The local time is $now. EOT my $DBH || DBI->connect( "dbi:mysql:localhost", "rentsaver", "ro +ot" ) ; Dumper( $DBH ); return OK; } 1;
am i missing something really obvious? or is there a bigger problem?

Replies are listed 'Best First'.
Re: accessing Apache::DBI 'cached connections'
by bobn (Chaplain) on Jul 21, 2003 at 20:56 UTC

    If you weren't using Apache::DBI, then your code:

    my $DBH || DBI->connect( "dbi:mysql:localhost", "rentsaver", "root" ) ;

    probably should have been:

    $DBH ||= DBI->connect( "dbi:mysql:localhost", "rentsaver", "root" ) ;

    You lost the equal part of the assignment operator. This code may create a connection, but it doesn't set $DBH.

    But since you are using Apache::DBI, it should probably be:

    $DBH = DBI->connect( "dbi:mysql:localhost", "rentsaver", "root" ) ;

    Also, your startup file need only use Apache::DBI, not DBI; Apache::DBI loads DBI, then overrides the connect and discoonnect methods to implement the caching for you.

    --Bob Niederman, http://bob-n.com
      well, i removed the double-load of DBI ...

      i should get *some* output from Data::Dumper if i've connected. another script on the same server ( non-mod_perl ) give me:

      $VAR1 = bless( {}, 'DBI::db' );
      so i'm lost ...

        Apache::DBI works transparently, so if one scripts works, and the other doesn't, then there must be differences in the way you connect in the two scripts. In other words a script that works without Apache::DBI must also work with Apache::DBI!

        Try the following bit of code and see what you get...

        #!/usr/bin/perl use CGI qw/:standard/; use DBI; use Data::Dumper; $Apache::DBI::DEBUG = 2; # Turn on Apache::DBI debugging print header, start_html('Testing Apache::DBI'), h1('Testing Apache::DBI'); my $dbh; eval { $dbh = DBI->connect( "DBI:mysql:database=test;host=localhost", undef +, undef, { RaiseError => 1 } ) ; }; if ($@) { print p('DBI Error: '.$@); } else { print p('Connect success'); print p("We are using Apache::DBI") if $dbh->isa('Apache::DBI::db'); print pre(Dumper($dbh)); } print end_html();

        When I call that with Apache::DBI enabled, I get the following:

        Testing Apache::DBI
        
        Connect success
        
        We are using Apache::DBI
        
        $VAR1 = bless( {}, 'Apache::DBI::db' );
        

        And in my error log I get:

        13692 Apache::DBI need ping: yes
        13692 Apache::DBI new connect to 'database=test;host=localhostAutoCommit=1PrintError=1RaiseError=1'
        

        Cheers,

        Cees

        Update: Looks like this got solved while I was writing my test script. I guess I'll have to be quicker next time...

        In your example, $DBH is just an empty lexical variable that you declared right before dumping it. The DBI->connect code will never even run because "my $DBH" will always return true. (It returns the result of the my() call, not the value of the variable.)

        UPDATE: Advice about the return value of my() is not correct. See below.

        Did you also fix the module code so that you are actually assigning the return value of DBI->connect to $DBH?

        --Bob Niederman, http://bob-n.com
Re: accessing Apache::DBI 'cached connections'
by hardburn (Abbot) on Jul 21, 2003 at 20:45 UTC

    ???

    my $DBH || DBI->connect( "dbi:mysql:localhost", "rentsaver", "ro +ot" ) ;

    There's your DBI right there. Except it should be my $DBH = DBI-> . . ..

    Update: Changed double '==' to a single '=' (oops!)

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      well, for one it should be a single =, not a double, i'd think.

      i 'borrowed' the line of code out of the _Writing ... _ book. supposedly there's a global $DBH variable.

      i've changed the || to =, but still no stuff in $DBH.

        No, you're reading something wrong. There is no global $DBH variable unless you declare one.

        You're right, should have been a single =.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        Note: All code is untested, unless otherwise stated

Re: accessing Apache::DBI 'cached connections'
by geektron (Curate) on Jul 21, 2003 at 20:47 UTC
    i did miss one obvious thing in the handler:
    print Dumper( $DBH );
    but i still have an empty database handle ...