Skip to content

Commit 97c9cb4

Browse files
authored
Add documentation about transaction modes (#155)
1. **Pass `mode: :immediate` to `Repo.transaction/2`:** Use this approach to set the transaction mode for individual transactions. 2. **Define custom transaction functions:** Create wrappers, such as `Repo.immediate_transaction/2` or `Repo.deferred_transaction/2`, to easily apply different modes where needed. 3. **Set a global default:** Configure `:default_transaction_mode` to apply a preferred mode for all transactions.
1 parent 19e1e47 commit 97c9cb4

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

lib/ecto/adapters/sqlite3.ex

+45-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ defmodule Ecto.Adapters.SQLite3 do
1313
1414
* `:database` - The path to the database. In memory is allowed. You can use
1515
`:memory` or `":memory:"` to designate that.
16+
* `:default_transaction_mode` - one of `deferred` (default), `immediate`,
17+
or `exclusive`. If a mode is not specified in a call to `Repo.transaction/2`,
18+
this will be the default transaction mode.
1619
* `:journal_mode` - Sets the journal mode for the sqlite connection. Can be
1720
one of the following `:delete`, `:truncate`, `:persist`, `:memory`,
1821
`:wal`, or `:off`. Defaults to `:wal`.
@@ -51,8 +54,8 @@ defmodule Ecto.Adapters.SQLite3 do
5154
* `:datetime_type` - Defaults to `:iso8601`. Determines how datetime fields are stored in the database.
5255
The allowed values are `:iso8601` and `:text_datetime`. `:iso8601` corresponds to a string of the form
5356
`YYYY-MM-DDThh:mm:ss` and `:text_datetime` corresponds to a string of the form `YYYY-MM-DD hh:mm:ss`
54-
* `:load_extensions` - list of paths identifying extensions to load. Defaults to []. The provided list will
55-
be merged with the global extensions list, set on :exqlite, :load_extensions. Be aware that the path should
57+
* `:load_extensions` - list of paths identifying extensions to load. Defaults to []. The provided list will
58+
be merged with the global extensions list, set on :exqlite, :load_extensions. Be aware that the path should
5659
handle pointing to a library compiled for the current architecture. See `Exqlite.Connection.connect/1` for more.
5760
5861
For more information about the options above, see [sqlite documentation][1]
@@ -177,11 +180,51 @@ defmodule Ecto.Adapters.SQLite3 do
177180
types, despite the fact SQLite only has [five storage classes][5]. The query will still
178181
work and return data, but you will need to do this mapping on your own.
179182
183+
### Transaction mode
184+
185+
By default, [SQLite transactions][8] run in `DEFERRED` mode. However, in
186+
web applications with a balanced load of reads and writes, using `IMMEDIATE`
187+
mode may yield better performance.
188+
189+
Here are several ways to specify a different transaction mode:
190+
191+
**Pass `mode: :immediate` to `Repo.transaction/2`:** Use this approach to set
192+
the transaction mode for individual transactions.
193+
194+
Multi.new()
195+
|> Multi.run(:example, fn _repo, _changes_so_far ->
196+
# ... do some work ...
197+
end)
198+
|> Repo.transaction(mode: :immediate)
199+
200+
**Define custom transaction functions:** Create wrappers, such as
201+
`Repo.immediate_transaction/2` or `Repo.deferred_transaction/2`, to easily
202+
apply different modes where needed.
203+
204+
defmodule MyApp.Repo do
205+
def immediate_transaction(fun_or_multi) do
206+
transaction(fun_or_multi, mode: :immediate)
207+
end
208+
209+
def deferred_transaction(fun_or_multi) do
210+
transaction(fun_or_multi, mode: :deferred)
211+
end
212+
end
213+
214+
**Set a global default:** Configure `:default_transaction_mode` to apply a
215+
preferred mode for all transactions, unless explicitly passed a different
216+
`:mode` to `Repo.transaction/2`.
217+
218+
config :my_app, MyApp.Repo,
219+
database: "path/to/my/database.db",
220+
default_transaction_mode: :immediate
221+
180222
[3]: https://www.sqlite.org/compile.html
181223
[4]: https://www.sqlite.org/whentouse.html
182224
[5]: https://www.sqlite.org/datatype3.html
183225
[6]: https://www.sqlite.org/datatype3.html#collating_sequences
184226
[7]: https://hexdocs.pm/ecto/schemaless-queries.html
227+
[8]: https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
185228
"""
186229

187230
use Ecto.Adapters.SQL,

0 commit comments

Comments
 (0)