What happens when you create nested dosync calls? Will sub-transactions be completed in the parent scope? Are these sub-transactions reversible if the parent transaction fails?
What happens when you create nested dosync calls? Will sub-transactions be completed in the
Share
If you mean syntactic nesting, then the answer is it depends on whether the inner
dosyncwill run on the same thread as the outer one.In Clojure, whenever a
dosyncblock is entered, a new transaction is started if one hasn’t been running already on this thread. This means that while execution stays on a single thread, inner transactions can be said to be subsumed by outer transactions; however if adosyncoccupies a position syntactically nested within anotherdosync, but happens to be launched on a new thread, it will have a new transaction to itself.An example which (hopefully) illustrates what happens:
The “inner” transaction retries after printing
:foo; the “outer” transaction never needs to restart. (Note that after this happens,r‘s history chain is grown, so if the “large”dosyncform were evaluated for a second time, the innerdosyncwould not retry. It still wouldn’t be merged into the outer one, of course.)Incidentally, Mark Volkmann has written a fantastic article on Clojure’s Software Transactional Memory; it’s highly recommended reading for anyone interested in gaining solid insight into details of this sort.