[Catalyst] Best practice for using transactions?

Brandon Black blblack at gmail.com
Fri Dec 15 14:13:31 GMT 2006


On 12/15/06, Jon Warbrick <jw35 at cam.ac.uk> 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.
>

I've never really found a good reason to run read transactions as
transactions.  In my experience it usually suffices to wrap up the
write transactions, and they usually occur in an isolated block of
code anyways.

Assuming you really do want to do what you're talking about, the best
method would probably be to write a Plugin, something like
Catalyst::Plugin::AutoTransaction::DBIC.  You could override
handle_request() and some exception stuff to plug in the txn magic.

Or on the other hand, you could probably do it as a Controller
base-class using the begin and end methods there too, and then you
still have the option of some readonly controller not using
transactions.

-- Brandon



More information about the Catalyst mailing list