This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-03-05
Channels
- # announcements (1)
- # asami (1)
- # babashka (7)
- # beginners (11)
- # biff (2)
- # calva (7)
- # cider (1)
- # clara (5)
- # clj-kondo (221)
- # clojure (83)
- # clojure-boston (3)
- # clojure-conj (1)
- # clojure-europe (9)
- # clojure-uk (1)
- # clojurescript (11)
- # cursive (74)
- # emacs (30)
- # figwheel-main (2)
- # fulcro (13)
- # hyperfiddle (6)
- # lsp (6)
- # malli (2)
- # nyc (4)
- # off-topic (78)
- # practicalli (9)
- # reitit (4)
- # sci (20)
- # shadow-cljs (49)
- # sql (9)
- # xtdb (5)
Hi! why is the transaction not rolling back? I'm using next.jdbc "1.3.847" and MySQL with InnoDb, which supports transactions, I have tried several ways but not success.
(deftest rollback-test
(let [db-spec {:jdbcUrl "jdbc:"}]
(is (= [#:product{:id_product 61}] (jdbc/execute! db-spec ["select id_product from product limit 1"])))
(binding [next.jdbc.transaction/*nested-tx* :ignore]
(jdbc/with-transaction [tx db-spec {:rollback-only true}]
(is (= [#:product{:id_product 61}] (jdbc/execute! tx ["select id_product from product limit 1"])))
(.setAutoCommit tx false)
(is (= false (.getAutoCommit tx)))
(jdbc/execute-one! tx ["truncate product"])
(.rollback tx)))
(is (= [#:product{:id_product 61}] (jdbc/execute! db-spec ["select id_product from product limit 1"])))))
Several questions here: why are you messing with auto commit? Why are you explicitly trying to roll back the transaction?
Have you tried delete from instead of truncate?
I ask the latter in case MySQL treats truncate as ddl rather than regular SQL - ddl can't be rolled back in transactions
I just tried with serveral ways, with explicit and implicit (commenting out) auto-commit and rollback Also tried with delete instead of truncate, also without the nested-tx binding but it doesn't rollback neither
(deftest rollback-test
(let [db-spec {:jdbcUrl "jdbc:"}]
(is (= [#:product{:id_product 61}] (jdbc/execute! db-spec ["select id_product from product limit 1"])))
(binding [next.jdbc.transaction/*nested-tx* :ignore]
(jdbc/with-transaction [tx db-spec {:rollback-only true}]
(is (= [#:product{:id_product 61}] (jdbc/execute! tx ["select id_product from product limit 1"])))
(jdbc/execute! tx ["delete from product where id_product = ?" 61])))
(is (= [#:product{:id_product 61}] (jdbc/execute! db-spec ["select id_product from product limit 1"])))))
Why are you binding the nested tx Var here? Is there an outer tx in your fixtures?
the db already has fix data, I have tried without the binding too, but it doesn't rollback either
ah my bad, found the issue as you guessed right, truncate doesn't work. I have tried again with delete without the binding and now it does the rollback
(deftest rollback-test
(let [db-spec {:jdbcUrl "jdbc:"}]
(is (= [#:product{:id_product 61}] (jdbc/execute! db-spec ["select id_product from product limit 1"])))
(jdbc/with-transaction [tx db-spec {:rollback-only true}]
(is (= [#:product{:id_product 61}] (jdbc/execute! tx ["select id_product from product limit 1"])))
(jdbc/execute! tx ["delete from product where id_product = ? " 61]))
(is (= [#:product{:id_product 61}] (jdbc/execute! db-spec ["select id_product from product limit 1"])))))