[Dbix-class] nested transactions and communicating to other processes

Dami Laurent (PJ) laurent.dami at justice.ge.ch
Thu Oct 23 08:32:14 GMT 2014


Hi there,

I encountered a delicate situation involving nested transactions. 
Consider a subroutine more or less like this :

  sub insert_and_publish {
    my @keys = $schema->txn_do(sub {some_complex_inserts()});
    publish_to_outer_world(@keys);
  }

This creates a bunch of records within a transaction. If the transaction succeeds, changes are commited to the database, and then we can publish the record keys to other processes. With one single transaction, everything is well. However, if this is called from another transaction, we get into trouble :

  $schema->txn_do(sub {
    insert_and_publish();
    do_some_more_stuff();
   });
   # only now did the real commit take place

The problem here is that because of nested transactions, the commit only takes place at the outermost level; so the sequence of operations in insert_and_publish() no longer works : other processes get notified about keys that have not been committed yet (because do_some_more_stuf() might take some time), or possibly will never be committed (because do_some_more_stuf() might fail).

The question is : how to rewrite insert_and_publish() so that it would work equally well in a single transaction, or in a nested transaction ? It seems that what is needed is a callback mechanism, to record things that should be done just after the transaction, and would be called automatically after commit time. Any suggestions ?

Thanks in advance, Laurent Dami



More information about the DBIx-Class mailing list