eval { local $dbh->{AutoCommit} = 0; }; $self->{_can_do_transactions} = $@ ? 0 : 1;

can be written more reliably as

$self->{_can_do_transactions} = eval { local $dbh->{AutoCommit} = 0; 1 };

But That boils down to the following code, which you might want to use directly.

$self->{_can_do_transactions} = eval { $dbh->begin_work(); $dbh->rollback() };

I don't see the point of determining the capability in advance, so we end up with:

my $in_trans = eval { $dbh->begin_work() }; if (!eval { code_that_touches_db(); $dbh->commit() if $in_trans; 1 }) { my $msg = $@; eval { $dbh->rollback() } if $in_trans; die $msg; }

Cleaner?

use Sub::ScopeFinalizer qw( scope_finalizer ); { my $in_trans = eval { $dbh->begin_work() }; my $pending_rollback = scope_finalizer { local $@; eval { $dbh->rollback() } if $in_trans; }; code_that_touches_db(); $pending_rollback->disable(); $dbh->commit() if $in_trans; }

Cleaner still?

BEGIN { package TransactionMaybe; sub begin { my ($class, $dbh) = @_; my $in_trans = eval { $dbh->begin_work() }; return bless([$dbh, $in_trans], $class); } sub commit { my ($self) = @_; my $dbh = $self->[0]; our $in_trans; local *in_trans = \($self->[1]); $dbh->commit() if $in_trans; $in_trans = 0; } sub DESTROY { my ($dbh, $in_trans) = @$self; local $@; eval { $dbh->rollback() } if $in_trans; } $INC{'TransactionMaybe.pm'} = 1; } { my $transaction = TransactionMaybe->begin($dbh); code_that_touches_db(); $transaction->commit(); }

However, it seems to me it's a bug to call call the same code_that_touches_db() whether transactions are available or not.


In reply to Re: How to safely test if a database handle is capable of transactions? by ikegami
in thread How to safely test if a database handle is capable of transactions? by larryl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.