Skip to content

Commit

Permalink
moves to txn
Browse files Browse the repository at this point in the history
  • Loading branch information
mylesmegyesi committed Sep 26, 2012
1 parent 4b9fb45 commit 3922e0a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
* Logging: For debugging. For runtime monitoring. For warning of dangerous actions.
* Connection pooling for Sql implementations
* Better transactions (i.e. use savepoints)
* Website?
* Indexes:
* Riak: currently indexes ALL fields. That's inefficient.
Expand Down
23 changes: 19 additions & 4 deletions sql/spec/hyperion/sql/jdbc_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
(count (execute-query
(make-query "SELECT * FROM test"))))

(defn do-query [query]
(execute-write
(make-query query)))

(describe "JDBC Adapter"
(with connection-url "jdbc:sqlite:")
(around [it]
Expand All @@ -33,17 +37,28 @@

(it "rolls back all changes"
(rollback
(execute-write
(make-query "INSERT INTO test (name, age) VALUES ('Myles', 23)")))
(should= 0 (test-count)))
(do-query "INSERT INTO test (name, age) VALUES ('Myles', 23)")
(should= 1 (test-count)))
(should= 0 (test-count)))

(it "resets auto commit to its previous value"
(.setAutoCommit (connection) true)
(try
(rollback
(throw (Exception.)))
(catch Exception _))
(should (.getAutoCommit (connection)))))
(should (.getAutoCommit (connection))))

(it "rolls back multiple"
(rollback
(do-query "INSERT INTO test (name, age) VALUES ('Myles', 23)")
(rollback
(do-query "INSERT INTO test (name, age) VALUES ('Myles', 23)")
(should= 2 (test-count)))
(should= 1 (test-count)))
(should= 0 (test-count)))

)

(context "transaction"
(it "commits"
Expand Down
2 changes: 1 addition & 1 deletion sql/spec/hyperion/sql/key_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
(should-not= (compose-key "foo" 1) (compose-key "bar" 1)))

(it "can parse a key"
(let [key (compose-key :foo 123)]
(let [key (compose-key "foo" 123)]
(should= ["foo" 123] (decompose-key key))))

)
42 changes: 37 additions & 5 deletions sql/src/hyperion/sql/jdbc.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns hyperion.sql.jdbc
(:use
[hyperion.key :only [generate-id]]
[hyperion.sql.connection :only [connection]]
[hyperion.sql.query :only [query-str params]]
[hyperion.sql.query-builder]))
Expand Down Expand Up @@ -133,14 +134,45 @@
(defmacro transaction [& body]
`(transaction-fn (fn [] ~@body)))

(def ^{:private true :dynamic true} *in-txn* false)

(defn with-txn [f]
(if *in-txn*
(f)
(binding [*in-txn* true]
(without-auto-commit
(try
(let [result (f)]
(.commit (connection))
result)
(catch Exception e
(.rollback (connection))
(throw e)))))))

(def ^{:private true :dynamic true} *in-rollback* false)

(defn new-savepoint-id [] (generate-id))

(defn exec [query]
(let [stmt (.createStatement (connection))]
(.executeUpdate stmt query)
(.close stmt)))

(defn- begin-savepoint []
(let [savepoint-id (new-savepoint-id)]
(exec (str "SAVEPOINT \"" savepoint-id "\""))))

(defn- rollback-to-savepoint [savepoint-id]
(exec (str "ROLLBACK TO SAVEPOINT \"" savepoint-id "\"")))

(defn rollback-fn [f]
(without-auto-commit
(try
(f)
(finally
(.rollback (connection))))))
(with-txn
(fn []
(let [savepoint-id (begin-savepoint)]
(try
(f)
(finally
(rollback-to-savepoint savepoint-id)))))))

(defmacro rollback [& body]
`(rollback-fn (fn [] ~@body)))

0 comments on commit 3922e0a

Please sign in to comment.