Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Difference in perl_mod

by himik (Acolyte)
on Nov 26, 2012 at 17:14 UTC ( [id://1005718]=perlquestion: print w/replies, xml ) Need Help??

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

Hello once again monkers!! I have this code

use DBI; sub simpleconnect { my $dbh; eval {$dbh=DBI->connect("dbi:Pg:dbname=$dbname;host=$host;port= +$port", $dbuser, $dbpass, {AutoCommit => 0});} if ($dbh) { $dbh->disconnect(); } } my $dbh; eval {$dbh=DBI->connect("dbi:Pg:dbname=$dbname;host=$host;port=$port +", $dbuser, $dbpass, {AutoCommit => 0});} if ($dbh) { &simpleconnect(); my $i=0; my while ($i<=7) { $result=0; my $sth=$dbh->prepare("INSERT INTO table1 (col1,col2) values +( ?, 'test')"); if ( $sth->execute($i) ) { $result=1 if ( $sth->rows()==1 ); } $sth->finish; } $dbh->commit() if ( $result ); $dbh->disconnect(); }
My question is why when i start the script through mod_perl in my table 1 is only 1 query the last one. And when i start it without mod_perl every thing is OK. I have all my rows in table1.

Replies are listed 'Best First'.
Re: Difference in perl_mod
by davido (Cardinal) on Nov 27, 2012 at 00:58 UTC

    There's one thing that jumps out at me that totally avoids dealing with the broken code you gave us to look at (which can't be the same code you're using to get the results you mentioned): In a persistent process such as mod_perl, you're going to suffer from the Monday Morning bug; the database connection eventually grows stale, the handle is left holding onto a dead line, and you will keep trying to use it. This is the problem that DBIx::Connector is designed to fix. Or a little less conveniently (imo), DBI's connect_cached() method.

    This probably has nothing to do with the problem you described as, "...when i start through mod_perl in my table 1 is only 1 query the last one.", but it will save you posting a second question in a few days. ;)


    Dave

      Thank you very much!
Re: Difference in perl_mod
by MidLifeXis (Monsignor) on Nov 26, 2012 at 18:22 UTC

    Where is $dbh getting assigned? I see where it is getting declared, but never assigned. Check the difference between your usage and the usage in DBI, especially in the connect call.

    Once you have that resolved, please explain what you think the call to simpleconnect() does, and why it is necessary.

    --MidLifeXis

      my mistake. I assign it in the eval

Re: Difference in perl_mod
by Anonymous Monk on Nov 26, 2012 at 21:51 UTC

    Well, I am, too, waiting for an explanation of simpleconnect(). Anyway,

    • use strict;
    • use warnings; and check error_log
    • my result=0; is missing a $
    • $result is scoped wrong
    • The eval appears to be useless (I don't recall DBI->connect dying without RaiseError)
    • You really ought to set RaiseError to 1.
    • Databases stop the transaction and refuse to continue when an error is encountered.
    • You ought to commit even with zero changed rows. (Or roll back.)
    • Check the error log for database errors.
Re: Difference in perl_mod
by Anonymous Monk on Nov 26, 2012 at 21:38 UTC
    Remember that mod_perl is a persistent environment: your application context never quite ends.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1005718]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found