Skip to content
Permalink
Browse files

Add transactions demo

  • Loading branch information
jcrossley3 committed Apr 3, 2015
1 parent 545dcf4 commit b4aa86223f27b2e496d21f505a4141ad063200db
Showing with 42 additions and 5 deletions.
  1. +1 −1 README.md
  2. +4 −2 project.clj
  3. +4 −2 src/demo/core.clj
  4. +33 −0 src/demo/transactions.clj
@@ -41,7 +41,7 @@ Once at a prompt, try `(demo.web/-main)`
Create an uberjar and run it

lein uberjar
java -jar target/demo-0.2.0-SNAPSHOT-standalone.jar
java -jar target/demo-standalone.jar

## In WildFly

@@ -4,13 +4,15 @@
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.6.0"]
[org.immutant/immutant "2.0.0-beta2"]
[org.immutant/immutant "2.x.incremental.542"]
[compojure "1.3.1"]
[ring/ring-devel "1.3.1"]
[org.clojure/core.memoize "0.5.6"]
[clj-time "0.9.0"]
[cheshire "5.4.0"]
[environ "1.0.0"]]
[environ "1.0.0"]
[org.clojure/java.jdbc "0.3.6"]
[com.h2database/h2 "1.3.176"]]
:repositories [["Immutant incremental builds"
"http://downloads.immutant.org/incremental/"]]
:plugins [[lein-immutant "2.0.0-beta1"]]
@@ -2,11 +2,13 @@
(:require demo.web
demo.scheduling
demo.messaging
demo.caching)
demo.caching
demo.transactions)
(:gen-class))

(defn -main [& args]
(apply demo.web/-main args)
(apply demo.messaging/-main args)
(apply demo.scheduling/-main args)
(apply demo.caching/-main args))
(apply demo.caching/-main args)
(apply demo.transactions/-main args))
@@ -0,0 +1,33 @@
(ns demo.transactions
(:require [immutant.transactions.scope :as tx]
[immutant.messaging :as msg]
[immutant.caching :as csh]
[clojure.java.jdbc :as sql]))

(def cache (delay (csh/cache (str *ns*) :transactional? true)))
(def queue (delay (msg/queue (str *ns*))))
(def db {:connection-uri "jdbc:h2:mem:demo;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"})

(defn unit-of-work [m]
(tx/not-supported
(csh/swap-in! @cache :attempts (fnil inc 0)))
(csh/swap-in! @cache :count (fnil inc 0))
(msg/publish @queue m)
(sql/insert! db :things m))

(defn dump []
(println "tx:queue =>" (msg/receive @queue :timeout -1))
(println "tx:cache =>" (into {} @cache))
(println "tx:db =>" (sql/query db ["select * from things"])))

(defn -main [& _]
(sql/db-do-commands db
(sql/create-table-ddl :things [:name "varchar(50)"]))

(tx/required (unit-of-work {:name "t-swizzle"}))
(dump)

(try
(tx/required (unit-of-work {:kanye "this should fail"}))
(catch Throwable e (println e)))
(dump))

0 comments on commit b4aa862

Please sign in to comment.
You can’t perform that action at this time.