[Catalyst] how to implement Transactions (over different db
tables) in a Catalyst project
Tomas Doran
bobtfish at bobtfish.net
Mon Jul 6 08:18:47 GMT 2009
On 6 Jul 2009, at 06:30, kakimoto at tpg.com.au wrote:
> In the past, when I had to work with just DBI, I would manually
> start a
> transaction and commit it at the end if no exceptions occur. In
> the case of the latter, I will call a rollback.
This is fairly easy to get wrong - txn_do forces you to have a block
which comprises the transaction scope, or your code won't compile.
It will start a transaction at the start of the block, and commit it
at the end if an exception hasn't been encountered during the
transaction
> How do I do it here in Catalyst/DbiX::Class land?
> I tried to read up the
> http://kobesearch.cpan.org/htdocs/DBIx-Class/DBIx/Class/
> Storage.html#txn_do
> doc but I can't see how the example can apply to Catalyst.
>
> It says, "$schema->txn_do($coderef)" to get the schema but in
> catalyst,
> we access the
> database tables directly. For example,
>
$c->model('myAppDB') gets the schema
>
> I'm a bit confused and have looked in a few places. Can anyone please
> educate me?
>
$c->model('myAppDB')->txn_do( sub {
my $rs = $c->model('myAppDB::Table')->search(..);
# Etc etc
die("Rollback") if $foo; # Exceptions cause rollback
# Etc etc
# If you get here, to the end, transaction is committed for you.
});
Can you please supply a doc patch for one or two of the places you
looked to find this information which will explain it more clearly
for the next person?
Cheers
t0m
More information about the Catalyst
mailing list