Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting aborted transactions in Clojure

I would like to analyze the behavior of a Clojure program versus a locking version in C. One metric I would like to track is the total number of aborted transactions for my Clojure program.

The only problem is that I can't mutate a variable outside of the context of the transaction that I am in. What I'm trying to do is this:

(dosync
(try
  (alter my_num inc)
  (catch Throwable t
    (do
      (alter total_aborts inc)
      (println "Caught " (.getClass t))
      (throw t)))))

Of course, total_aborts will never get incremented if the transaction doesn't finish!!! So how can I do this? Thanks!

like image 892
Timoteo Avatar asked Nov 14 '11 17:11

Timoteo


1 Answers

You can make my-num and total-aborts atoms and use swap! instead of alter.

like image 108
Matthias Benkard Avatar answered Nov 20 '22 22:11

Matthias Benkard