tomekw/hikari-cp

How to make a transaction ?

arlicle opened this issue · 3 comments

(jdbc/with-db-transaction [t-con db-spec]
  (jdbc/update! t-con :fruit
                {:cost 49}
                ["grade < ?" 75])
  (jdbc/execute! t-con
                 ["update fruit set cost = ( 2 * grade ) where grade > ?" 50.0]))

the db-spec is like this

(def db-spec
  {:adapter       "mysql"
   :username      "root"
   :password      "123"
   :database-name "projectx2"
   :server-name   "localhost"
   :port-number   3306
   :use-ssl       false})

I get this error

db-spec {:adapter "mysql", :username "root", :password "123", :database-name "projectx2", :server-name "localhost", :port-number 3306, :use-ssl false} is missing a required parameter

The example above doesn't appear to be using hikari-cp.

Does the following work?

(def db-spec
  {:adapter       "mysql"
   :username      "root"
   :password      "123"
   :database-name "projectx2"
   :server-name   "localhost"
   :port-number   3306
   :use-ssl       false})

(defonce datasource
  (delay (hikari/make-datasource db-spec)))

(jdbc/with-db-transaction [t-con {:datasource @datasource}]
  (jdbc/update! t-con :fruit
                {:cost 49}
                ["grade < ?" 75])
  (jdbc/execute! t-con
                 ["update fruit set cost = ( 2 * grade ) where grade > ?" 50.0]))

It's working, thank you very much:)

Happy to help :)