Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
The following code when the 'update stock' condition returns 0, executes sql_error()'s sql 'insert into sql_error' AND dosql()'s 'insert into stock_valid' ,despite that the first statement of sql_error() is $dbh->rollback. I know that wrapping do_sql()'s sql statements in $dbh->begin_work would probably make it work, but I'm interested in the background of why it is happening.
I have a theory that every 'do' statement does start a new implicit transaction. So let's say that do(insert into stock_valid) is T1 and do(update stock) is T2. Then, T1 is rolled back when the rollback of sql_error() is hit, while T2 is not rolled back and instead is committed when $dbh->commit; of sql_error() is hit.
My 2cents. What do you think?
$dbh->{AutoCommit}=0; $dbh->{RaiseError}=1; $dbh->{HandleError}=\&handle_error; sub do_sql() { $dbh->do(qq{ insert into stock_valid (last_year,orderid,suppliervatno, code,executedquantity,productprice) values ( $last_year,$orderid,$suppliervatno, $code,$executedquantity,$productprice ) }); my $rowsupdated=$dbh->do(qq{ update stock set quantity=quantity+$executedquantity where code=$code }); if ($rowsupdated==0) { sql_error($order,'no rows found','LocalError'); } $dbh->commit; $dbh->disconnect; } sub sql_error() { $dbh->rollback; my $orderid=$dbh->quote(shift); my $resultreason=$dbh->quote(shift); my $resultcode=$dbh->quote(shift); $dbh->do(qq{ insert into sql_error (orderid,resultreason,resultcode) values ( $orderid,$resultreason,$resultcode ) }); $dbh->commit; $dbh->disconnect; die; }
Grandfather replaced pre tags with Code tags and fixed paragraphing.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Does each DBI 'do' start a new transaction?
by ikegami (Patriarch) on Mar 19, 2024 at 19:34 UTC | |
|
Re: Does each DBI 'do' start a new transaction?
by talexb (Chancellor) on Mar 19, 2024 at 16:30 UTC | |
by ikegami (Patriarch) on Mar 19, 2024 at 19:36 UTC | |
by talexb (Chancellor) on Mar 20, 2024 at 13:26 UTC | |
by etj (Priest) on Nov 07, 2024 at 16:04 UTC | |
|
Re: Does each DBI 'do' start a new transaction?
by LanX (Saint) on Mar 19, 2024 at 14:34 UTC | |
by Anonymous Monk on Mar 19, 2024 at 15:50 UTC |