问题描述
当您创建嵌套的 dosync 调用时会发生什么?子事务会在父作用域内完成吗?如果父事务失败,这些子事务是否可逆?
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?
推荐答案
如果你说的是句法嵌套,那么答案是这取决于内部 dosync
是否会在同一个线程上运行作为外层.
If you mean syntactic nesting, then the answer is it depends on whether the inner dosync
will run on the same thread as the outer one.
在 Clojure 中,每当进入 dosync
块时,就会启动一个新事务如果尚未在此线程上运行.这意味着虽然执行停留在单个线程上,但可以说内部事务被外部事务包含;然而,如果一个 dosync
占据了一个在语法上嵌套在另一个 dosync
中的位置,但碰巧在一个新线程上启动,它将有一个新的事务给自己.
In Clojure, whenever a dosync
block 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 a dosync
occupies a position syntactically nested within another dosync
, but happens to be launched on a new thread, it will have a new transaction to itself.
一个(希望)说明会发生什么的例子:
An example which (hopefully) illustrates what happens:
user> (def r (ref 0))
#'user/r
user> (dosync (future (dosync (Thread/sleep 50) (println :foo) (alter r inc)))
(println :bar)
(alter r inc))
:bar
:foo
:foo
1
user> @r
2
内部"事务在打印:foo
后重试;外部"事务永远不需要重新启动.(注意,在这发生之后,r
的历史链会增长,所以如果大"dosync
表单被第二次评估,内部 dosync
不会重试.当然,它仍然不会合并到外部.)
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" dosync
form were evaluated for a second time, the inner dosync
would not retry. It still wouldn't be merged into the outer one, of course.)
顺便说一下,Mark Volkmann 写了一篇关于 Clojure 的软件事务内存的精彩文章;强烈建议任何有兴趣深入了解此类细节的人阅读.
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.
这篇关于嵌套的 dosync 调用的行为如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!