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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |