Transactions - How the DBI helps
Tools of the trade:
- Set AutoCommit off, and RaiseError on
- Wrap eval { … } around the code
- Use $dbh->commit; and $dbh->rollback;
Disable AutoCommit via $dbh->{AutoCommit} = 0;
- to enable transactions and thus rollback-on-error
Enable RaiseError via $dbh->{RaiseError} = 1;
- to automatically 'throw an exception' after an error
The surrounding eval { … }
- catches the exception, the error text is stored in $@
Test $@ and $dbh->rollback() if set
- note that a failed statement doesn’t automatically trigger a rollback