Clojure: Transactions
This page is WORK IN PROGRESS.
States, Transactions
- refs
-
{synchronous, coordinated}.
ref
- agents
-
{asynchronous, independent}.
agent
- atoms
-
{synchronous, independent}.
atom
- var
-
like global var.
def
“refs” is typical set variable.
(deref val)
is same as @val
Transactions
“transactions” is like database transactions. Process inside a transaction happen together or not at all, there is no possibility of partial success. (this property is often called “atomicity”)
“transactions” are isolated. No 2 transactions see each other.
dosync
creates a transaction. Other wise it's similar to do
.
ref-set
is for setting a ref. It can only be used within a transaction.
alter
is like ref-set
, except it takes a function and other args.
clojure.core/commute
clojure.core/ensure
atom
atom is like ref, except it's rather for independent entities (update that do not need to be coordinated with others).
clojure.core/atom
clojure.core/swap!
clojure.core/reset!
agents (asynchronous)
most useful.
clojure.core/agent
thread-local state: var, def
clojure.core/bind
validator and watch
clojure.core/set-validator!
clojure.core/add-watch