Skip to content

Commit

Permalink
Add transactions demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrossley3 committed Apr 3, 2015
1 parent 545dcf4 commit b4aa862
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions project.clj
Expand Up @@ -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"]]
Expand Down
6 changes: 4 additions & 2 deletions src/demo/core.clj
Expand Up @@ -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))
33 changes: 33 additions & 0 deletions src/demo/transactions.clj
@@ -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.