in reply to seeking barebones sqlite example with transaction
The portable way to do transactions with DBI is by using begin_work, and commit/rollback.
Usually you'd wrap the transaction in an eval to catch errors, so the following would be the more common idiom (copied from the Transactions section in the DBI docs):
$dbh->begin_work; eval { local $dbh->{RaiseError} = 1; foo(...) # do lots of work here bar(...) # including inserts baz(...) # and updates $dbh->commit; # commit the changes if we get this far }; if ($@) { warn "Transaction aborted because $@"; # now rollback to undo the incomplete changes # but do it in an eval{} as it may also fail eval { $dbh->rollback }; # add other application on-error-clean-up code here }
Update: Added RaiseError code per runrig's comment.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: seeking barebones sqlite example with transaction
by runrig (Abbot) on Aug 04, 2006 at 22:19 UTC | |
by rhesa (Vicar) on Aug 04, 2006 at 22:28 UTC |