I have read through the DBI docs and DBD::mysql docs a couple times and
think I have a grasp of this whole deal but thought I would see what the
gurus have to say on the matter. For reference I am at Perl 5.8.1, DBI
1.38, DBD::mysql 2.9002, mysql 3.23.56.  I normally handle my own errors
rather than using RaiseError and have AutoCommit set to the default. To
prevent having to rewrite a bunch of other code at the moment my goal is
to leave it that way and "just do the right thing" where I need
transaction support. My code model (pseudo) at this point looks like:

$dbh holds an already initialized database handle

# localize the setting of dbh handle attributes
{
    local $dbh->{AutoCommit} = 0;
    local $dbh->{RaiseError} = 1;

    # now perform our steps inside of an eval, allowing us to catch the 
    # exceptions triggering a rollback

    my $sth;
    eval {
        my $st = "SELECT * ....";
        my @bind = ( bind values );

        $sth = $dbh->prepare($st);
        $sth->execute(@bind);

        $dbh->commit;
    }
    if ($@) {
        $dbh->rollback;
        # throw my normal exception stuffs here
    }
}

Questions:
1) Can/should I do the 'local' setting of the attributes within the eval
block without causing any other issues?  I can't think of reasons not
to, but maybe you can?

2) If the answer to number 1 is no, then presumably 'rollback' throws an
exception itself so I would have to catch it too with an 'eval'?  Can
rollback fail? If I can move the settings inside of the 'eval' then
rollback failing should do "normal" error reporting correct, is that
returning undef or true?

3) Similar to #2, setting AutoCommit to 0 before setting the RaiseError
would mean I need to check that it succeeds (which is just checking the
boolean context of the attribute) moving it into the eval should mean I
can put it after the RaiseError and let the eval catch its exception as
well, correct?

Just trying to get this whole thing clear, thanks for your time,

http://danconia.org

Reply via email to