Funny thing, finding a good pattern for transaction management was actually 
harder than implementing migrations. The problem is that with RaiseError 
the $db object goes out of scope and puts the database handle, which looks 
fine, back into the connection cache with a transaction that's still in 
progress. So what i ended up with is the transaction scope guard approach 
from DBIx::Class, which as it turns out, works extremely well with async 
queries as well.

  # Commit
  {
    my $tx = $db->begin;
    $db->query('insert into foo values (?)', 'one');
    $db->query('insert into foo values (?)', 'two');
    $tx->commit;
  };

 If $tx goes out of scope before $tx->commit has been called, it will 
trigger a rollback, super simple.

  # Rollback
  {
    my $tx = $db->begin;
    $db->query('insert into foo values (?)', 'one');
    $db->query('insert into foo values (?)', 'two');
  };

This is the latest addition in Mojo::Pg 0.07, which i've just released.

--
sebastian

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to