If AutoCommit is on, begin_work() will turn it off, until the transaction gets commited or rolled back, so this code works:

use DBI; use strict; use warnings; my $dsn = "DBI:mysql:database=test;host=blah"; my $dbh = DBI->connect($dsn, 'root', 'abcd', {RaiseError => 1, AutoCom +mit => 1}); my $command = "update test set a = a + 1"; my $sth = $dbh->prepare($command); $dbh->begin_work(); $sth->execute(); $sth->execute(); $sth->execute(); $sth->execute(); $dbh->commit();

If AutoCommit is off, then you are sort of in an implicit transaction already, and when you call begin_work is issues an error. I personally don't like this behavior, but this is how it works. If you run this code (the only difference is that AutoCommit is off:

use DBI; use strict; use warnings; my $dsn = "DBI:mysql:database=test;host=blah"; my $dbh = DBI->connect($dsn, 'root', 'abcd', {RaiseError => 1, AutoCom +mit => 0}); my $command = "update test set a = a + 1"; my $sth = $dbh->prepare($command); $dbh->begin_work(); $sth->execute(); $sth->execute(); $sth->execute(); $sth->execute(); $dbh->commit();

You get this error:

DBD::mysql::db begin_work failed: Already in a transaction at math1.pl + line 10. DBD::mysql::db begin_work failed: Already in a transaction at math1.pl + line 10.

Summary: the logic behind is, regardless whether I like it: If AutoCommit is on, and you want a transaction, obviously I have to turn it off for you as long as you remain in the transaction, so that you can decide the poitn to commit or rollback; But if AutoCommit is off, you are already in sort of transaction, why you start a transaction in a transaction?


In reply to Re: odd transaction behavior: dbi mysql by pg
in thread odd transaction behavior: dbi mysql by nmerriweather

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.