Correct documentation for transact and with-transaction

Previously both functions were documented as taking a `Connectable`
object. This amends the documentation as well as the fn signature to
take a `Transactable` object.
This commit is contained in:
jet 2019-06-02 12:03:14 -04:00
parent cbf10e581b
commit df1c38c03f
3 changed files with 14 additions and 12 deletions

View file

@ -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. 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. 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.

View file

@ -183,17 +183,17 @@
(assoc opts :next.jdbc/sql-params sql-params)))) (assoc opts :next.jdbc/sql-params sql-params))))
(defn transact (defn transact
"Given a connectable object and a function (taking a `Connection`), "Given a transactable object and a function (taking a `Connection`),
execute the function on a new connection in a transactional manner. execute the function over the connection in a transactional manner.
See `with-transaction` for supported options." See `with-transaction` for supported options."
([connectable f] ([transactable f]
(p/-transact connectable f {})) (p/-transact transactable f {}))
([connectable f opts] ([transactable f opts]
(p/-transact connectable f opts))) (p/-transact transactable f opts)))
(defmacro with-transaction (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 then executes the `body` in that context, committing any changes if the body
completes successfully, otherwise rolling back any changes made. completes successfully, otherwise rolling back any changes made.
@ -202,5 +202,5 @@
`:repeatable-read`, `:serializable`, `:repeatable-read`, `:serializable`,
* `:read-only` -- `true` / `false`, * `:read-only` -- `true` / `false`,
* `:rollback-only` -- `true` / `false`." * `:rollback-only` -- `true` / `false`."
[[sym connectable opts] & body] [[sym transactable opts] & body]
`(transact ~connectable (^{:once true} fn* [~sym] ~@body) ~opts)) `(transact ~transactable (^{:once true} fn* [~sym] ~@body) ~opts))

View file

@ -45,6 +45,8 @@
(s/def ::key-map (s/map-of keyword? any?)) (s/def ::key-map (s/map-of keyword? any?))
(s/def ::opts-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/def ::sql-params (s/and vector?
(s/cat :sql string? (s/cat :sql string?
:params (s/* any?)))) :params (s/* any?))))
@ -80,14 +82,14 @@
:opts (s/? ::opts-map)))) :opts (s/? ::opts-map))))
(s/fdef jdbc/transact (s/fdef jdbc/transact
:args (s/cat :connectable ::connectable :args (s/cat :transactable ::transactable
:f fn? :f fn?
:opts (s/? ::opts-map))) :opts (s/? ::opts-map)))
(s/fdef jdbc/with-transaction (s/fdef jdbc/with-transaction
:args (s/cat :binding (s/and vector? :args (s/cat :binding (s/and vector?
(s/cat :sym simple-symbol? (s/cat :sym simple-symbol?
:connectable ::connectable :transactable ::transactable
:opts ::opts-map)) :opts ::opts-map))
:body (s/* any?))) :body (s/* any?)))