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

Hi Monks,

I face quite a big problem at the time that would force me to rewrite loads of code if i can't find an answer :-(

I'm working on Apache 1.3 with mod_perl and Postgres 7.2.

before using ApacheDBI, I was caching my $dbh with an home made singleton object.

I used to open connections with AutoCommit On, change AutoCommit on the fly this way :

my $dbh = DBI->connect( ... {AutoCommit => 1, ..} ) ... >> perform some method call here < < local $dbh->{AutoCommit} = 0; eval { # call to EmitSignal on the parent object $this->EmitSignal( $event_id, @extra_args ); # call to Update on another object call "Thread" $thread->Update(); #Serialize thread. $dbh->commit(); }; if ($@) { $dbh->rollback(); die $@; } local $dbh->{AutoCommit} = 1; ...
This was working fine, each of those (this->EmitSignal and $thread->Update) was retrieving the same $dbh from the singleton, autocommit was off and $dbh->commit was commiting all.

When i was logging $dbh->{AutoCommit}, there was no value (==0)

Since i installed ApacheDBI, this doesn't work anymore. When i log the $dbh retrieved from the different objects (Thread and the parent object), i've got the same reference, something like Apache::DBI::db=HASH(0x8f1b840) for all of them.

but my error_log gives me Warning in Perl code: commit ineffective with AutoCommit enabled at /home/httpd_akio/pgakio/lib/perl/Akio/ThreadManager.pm line 170

when i log $dbh->{AutoCommit} from Thread object for example, it's 1 and not 0!!

It seems that the $dbh->{AutoCommit} = 0; only affects the $dbh used in the block, and not the one used in the subroutines, something like if the Apache::DBI retrieve from the subroutines was the one created by the connect_on_init from the startup.pl.

The doc says : However, when the $dbh attribute is altered after connect() it affects all other handlers retrieving this database handle. Therefore it's best to restore the modified attributes to their original value at the end of database handle usage.

With further investigation in ApacheDBI, it seems that all the handlers are cached in global %Apache::DBI::Connected. So you only modify the local copy that is send by Apache::DBI.

Isn't it possible to change parameters of the cached handlers??

Am i missing someting?

Isn't it possible to affect directly the Apache::DBI object ?

Thanks a lot for your help monks.

Christophe

janitored by ybiC: <code> tags, as per Monastery convention

Replies are listed 'Best First'.
Re: mod_perl ApacheDBI
by perrin (Chancellor) on Nov 12, 2003 at 18:51 UTC
    It sounds like you've understood it correctly: Apache::DBI will give you the same object back if you call connect with the same params, and setting AutoCommit on that object will set it for everyone, until you change it back.

    I suspect the problem has to do with inconsistent calls to connect. How does your $thread->Update method get its $dbh? Is it different from what you did before? If you turn on Apache::DBI debugging do you see it making new connections in your subs?

      Thanks for your answer. When i look at the error_log after enabling $Apache::DBI::DEBUG, i see that Apache DBI is always giving me the same handler with the default parameters parameters from my DBI Connect :

      I use a Database object to encapsulate the call to DBI. Here is the constructor :

      sub new {
      my $pkg = shift;

      my $user = $ENV{DB_USER} ;
      my $password = $ENV{DB_PASSWORD} ;
      my $dsn = $ENV{DB_DSN} ;
      my $dbh = DBI->connect($dsn,$user, $password, { AutoCommit => 1, RaiseError => 1 } )||
      die "ERROR NO_CONNECTION_TO_POSTMASTER\n";

      my $obj = {_dbh => $dbh};

      bless $obj, $pkg;

      return $obj;

      }

      And all my other objects calls this contructor with :

      my $db = new Database();

      Then when i need to change autocommit parameters, i do a simple $db->{_dbh}->{AutoCommit} = 0;

      What i can see is that i can't modify the database handlers pool from Apache::DBI, all i can do is modify my local copy.

      Next time one of my object ask for the same DBI handler, i get the original with AutoCommit == 1;

      Christophe
Re: mod_perl ApacheDBI
by jmanning2k (Pilgrim) on Nov 12, 2003 at 21:47 UTC
    I find the use of local a little curious. I think this may work if you put the first local statement inside the eval block instead of before the eval block. See Temporary Values via local()

    You should not put local $dbh->{AutoCommit} = 0 after the block. If you have a local inside the eval block, then just closing the block and letting it expire is enough.

    ~J

      The local must go outside the eval block. Otherwise, AutoCommit will be restored when leaving the eval block. When AutoCommit changes from off to on, DBI says that there will be a commit. This includes when the eval block is terminated with an exception. The automatic commit defeats the whole purpose of wrapping it in an eval and checking for a rollback.

      It is a bad idea to set the AutoCommit back to 1 at the end. Leaving the enclosing scope with reset it in any case. Also, it assumes that the AutoCommit defaults to on. One of the nice things about the transaction structure is that it works right regardless of the initial setting of AutoCommit.