concept transaction in category clojure

appears as: transaction, trsaction, trsaction, trsactions
The Joy of Clojure, Second Edition

This is an excerpt from Manning's book The Joy of Clojure, Second Edition.

Listing 10.3. Using alter to update refs in a transaction

Clearly something has gone awry. Despite using refs for all of the mutable state, we ended up with two white kings on the board and lost the black king entirely. If you run this code on your own machine, you may see something slightly different (for example, two black kings), but in all likelihood what you see will be wrong. The reason lies in splitting the updates of the to and from refs into different transactions described by the two separate uses of dosync in make-move. Judging by its behavior, this is not correct; but to understand why requires an understanding of software transactional memory, starting with what exactly a transaction is.

In systems that provide embedded transactions, it’s common for transactions to be nested, thus limiting the scope of restarts (Gray 1992). Embedding transactions in Clojure operate differently, as summarized in figure 10.6.

Figure 10.6. A restart in any of Clojure’s embedded transactions A, B, b, or C causes a restart in the entire subsuming transaction. This is unlike a fully embedded transaction system, where the subtransactions can be used to restrain the scope of restarts.

10.4.2. Using atoms in transactions

Just because we said that atoms should be used carefully in transactions, that’s not to say they can never be used that way. In fact, the use of an atom as the reference holding a function’s memoization cache is idempotent on update.

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest