[Catalyst] Best practice for using transactions?

Matt S Trout dbix-class at trout.me.uk
Sun Dec 17 14:12:51 GMT 2006


On 15 Dec 2006, at 13:03, Jon Warbrick wrote:

> Does anyone have any advice on best practise for using transactions  
> in a database-based Catalyst app?
>
> I'm using DBIx::Class, and I can see how I can wrap particular  
> processing steps with txn_do to impliment transactions. But it  
> seems to me that there might, for a general CRUD application, be  
> value in processing each request entirely within a transaction by  
> default - read-only requests would then get a consistent view of  
> the database, and write requests would be all-or-nothing by default.
>
> Trouble is, I can't see how to do this. txn_do seems to be a non- 
> starter (no appropriate coderef to wrap), and I can't see where I  
> could appropriately call txn_begin/txn_commit/txn_rollback to  
> achieve a useful effect.
>
> Any advice from people who've been here before (even if it's 'you  
> don't want to do that') would be very welcome.

You could always subclass Catalyst::Controller's _DISPATCH private  
action like

sub _DISPATCH {
   my $self = shift;
   my ($c) = @_;
   $c->model('DB')->schema->txn_do(
     sub {
       $self->next::method(@_);
     }
   );
}

-- 
Matt S Trout, Technical Director, Shadowcat Systems Ltd.
Offering custom development, consultancy and support contracts for  
Catalyst,
DBIx::Class and BAST. Contact mst (at) shadowcatsystems.co.uk for  
details.
+ Help us build a better perl ORM: http://dbix- 
class.shadowcatsystems.co.uk/ +





More information about the Catalyst mailing list