How can I see the number of rollbacks in my STM in Clojure?

You can't... unless you are willing to cheat:

(defmacro spy-dosync [& body]
  `(let [retries# (atom -1)
         result# (dosync
                   (swap! retries# inc)
                   ~@body)]
     (println "retries count:" @retries#)
     result#))

and then replace your dosync by a spy-dosync.


If you're feeling frisky, you could hack the Clojure source and rebuild (it's easy to rebuild the Clojure source). Transaction retries happen in src/jvm/clojure/lang/LockingTransaction.java in the run() method. There's a big for loop there that goes until done or RETRY_LIMIT. The value of i when the loop exits should be the retry count.

Tags:

Clojure

Stm