diff --git a/doc/transactions.md b/doc/transactions.md index c4da9a4..dbaaa14 100644 --- a/doc/transactions.md +++ b/doc/transactions.md @@ -2,7 +2,7 @@ The `transact` function and `with-transaction` macro were briefly mentioned in the [Getting Started](/doc/getting-started.md) section but we'll go into more detail here. -Although `(transact connectable thunk)` is available, it is expected that you will mostly use `(with-transaction [tx connectable] body...)` when you want to execute multiple SQL operations in the context of a single transaction so that is what this section focuses on. +Although `(transact transactable thunk)` is available, it is expected that you will mostly use `(with-transaction [tx transactable] body...)` when you want to execute multiple SQL operations in the context of a single transaction so that is what this section focuses on. By default, all connections that `next.jdbc` creates are automatically committable, i.e., as each operation is performed, the effect is committed to the database directly before the next operation is performed. Any exceptions only cause the current operation to be aborted -- any prior operations have already been committed. diff --git a/src/next/jdbc.clj b/src/next/jdbc.clj index 7c31036..99a2aec 100644 --- a/src/next/jdbc.clj +++ b/src/next/jdbc.clj @@ -183,17 +183,17 @@ (assoc opts :next.jdbc/sql-params sql-params)))) (defn transact - "Given a connectable object and a function (taking a `Connection`), - execute the function on a new connection in a transactional manner. + "Given a transactable object and a function (taking a `Connection`), + execute the function over the connection in a transactional manner. See `with-transaction` for supported options." - ([connectable f] - (p/-transact connectable f {})) - ([connectable f opts] - (p/-transact connectable f opts))) + ([transactable f] + (p/-transact transactable f {})) + ([transactable f opts] + (p/-transact transactable f opts))) (defmacro with-transaction - "Given a connectable object, gets a new connection and binds it to `sym`, + "Given a transactable object, gets a connection and binds it to `sym`, then executes the `body` in that context, committing any changes if the body completes successfully, otherwise rolling back any changes made. @@ -202,5 +202,5 @@ `:repeatable-read`, `:serializable`, * `:read-only` -- `true` / `false`, * `:rollback-only` -- `true` / `false`." - [[sym connectable opts] & body] - `(transact ~connectable (^{:once true} fn* [~sym] ~@body) ~opts)) + [[sym transactable opts] & body] + `(transact ~transactable (^{:once true} fn* [~sym] ~@body) ~opts)) diff --git a/src/next/jdbc/specs.clj b/src/next/jdbc/specs.clj index 2c08ceb..18b56a9 100644 --- a/src/next/jdbc/specs.clj +++ b/src/next/jdbc/specs.clj @@ -45,6 +45,8 @@ (s/def ::key-map (s/map-of keyword? any?)) (s/def ::opts-map (s/map-of keyword? any?)) +(s/def ::transactable any?) + (s/def ::sql-params (s/and vector? (s/cat :sql string? :params (s/* any?)))) @@ -80,14 +82,14 @@ :opts (s/? ::opts-map)))) (s/fdef jdbc/transact - :args (s/cat :connectable ::connectable + :args (s/cat :transactable ::transactable :f fn? :opts (s/? ::opts-map))) (s/fdef jdbc/with-transaction :args (s/cat :binding (s/and vector? (s/cat :sym simple-symbol? - :connectable ::connectable + :transactable ::transactable :opts ::opts-map)) :body (s/* any?)))