[Catalyst] How safe are database transactions?
Chisel Wright
chisel at herlpacker.co.uk
Thu Jan 4 11:52:20 GMT 2007
On Thu, Jan 04, 2007 at 11:11:10AM +0000, Ash Berlin wrote:
> $c->model('MyAppDB')->txn_do(sub {
> # Do some insertions here....
> });
Yes, this is pretty much how I do it too, e.g.:
sub _add_new_reply {
my ($self, $c) = @_;
my ($new_reply);
# some checks
# otherwise, the form data is ok ...
else {
# try to add the reply to the thread
eval {
$new_reply = $c->model('ParleyDB')->schema->txn_do(
sub { return $self->_txn_add_new_reply($c) }
);
};
# deal with any transaction errors
if ($@) { # Transaction failed
die "something terrible has happened!" #
if ($@ =~ /Rollback failed/); # Rollback failed
$c->stash->{error}{message} =
qq{Database transaction failed: $@};
$c->log->error( $@ );
return;
}
}
}
[source: http://xrl.us/t4w3]
and then all the db manipulation goes into "sub _txn_add_new_reply{...}"
I think this is pretty much how the docs recommend to do it. I've not
yet thought of a cleaner way to do this.
Chiz
--
Chisel Wright
e: chisel at herlpacker.co.uk
w: http://www.herlpacker.co.uk/
Q: What's tiny and yellow and very, very, dangerous?
A: A canary with the super-user password.
More information about the Catalyst
mailing list