in reply to accessing Apache::DBI 'cached connections'

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

Replies are listed 'Best First'.
Re: Re: accessing Apache::DBI 'cached connections'
by geektron (Curate) on Jul 21, 2003 at 21:10 UTC
    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.

        Your post sounded so good, I  ++'d it, but it's wrong.

        #!/usr/bin/perl -w my $DBH ||= retval('1'); print "$DBH\n"; sub retval { return $_[0] }

        This prints '1'.

        Update: Oddly enough, Camel 3 calls 'my' a named unary operator and lists named unary operators at higher precedence than any of assignment, assignment operators or logical operators, so you should be right, but the code doesn't work that way for me, at least not on 5.8.0 or 5.005_03.

        Newer Update: 'my' probably does have higher precedence thatn ||=, but it doesn't matter because it returns false.

        my $orr || print "or\n"; my $andd && print "and\n";

        prints 'or'.

        --Bob Niederman, http://bob-n.com

      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
        i had the connect string completely wrong.

        it's fixed now, and reads:

        $DBH = DBI->connect( "dbi:mysql:rentsavers", "root", "", ) ;
        i can't believe i blew the $dsn ....
        yes, i changed it to:
        $DBH = DBI->connect( "dbi:mysql:localhost", "rentsavers", "root", "", +) ;
        but i still get nothing ....