From http://clojure.org/refs : "I/O and other activities with
side-effects should be avoided in transactions, since transactions
will be retried. The io! macro can be used to prevent the use of an
impure function in a transaction."
Isn't it OK to do I/O in a transaction as long as doing the operation
multiple times has the same effect as doing it once (i.e. it's
idempotent)?
Isn't it also OK to read data in a transaction, in many circumstances?
Even if retrying the reads results in a different end state, that's OK
for lots of systems. Example: couldn't I safely read a stock price
from a ticker stream within a transaction, if the purpose was only to
have a cached value from sometime in the last 5 minutes?
(I imagine there are performance considerations. Retrying some I/O
operations could be expensive. Right now, I'm concerned with the
logic, not the performance, but if you know something about the
performance implications, please jump in).
Just hoping someone can straighten me out if I'm wrong... I'd like to
remove some io! macros from my code!
Thanks,
Hugh
> From http://clojure.org/refs : "I/O and other activities with
> side-effects should be avoided in transactions, since transactions
> will be retried. The io! macro can be used to prevent the use of an
> impure function in a transaction."
>
> Isn't it OK to do I/O in a transaction as long as doing the operation
> multiple times has the same effect as doing it once (i.e. it's
> idempotent)?
I suspect that Rich used the word "should" there rather than "must" as
a nod to cases like those you described. Any activities with side-
effects whose correctness will not be harmed if they are repeated
(potentially many times) are OK to include in a transaction.
Ultimately program correctness is up to you. The purpose of the
admonition is to put you on notice that the probability of retries for
code in transactions (over time) is approximately 100%. Any side
effects you include in them need to be carefully considered and will
work properly only if they are truly safe if repeated.
--Steve