-
Notifications
You must be signed in to change notification settings - Fork 344
Add adapter level read-only transaction support #741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -278,7 +278,7 @@ defmodule Ecto.Adapters.SQL do | |
|
|
||
| @impl true | ||
| def transaction(meta, opts, fun) do | ||
| Ecto.Adapters.SQL.transaction(meta, opts, fun) | ||
| Ecto.Adapters.SQL.transaction(meta, @conn, opts, fun) | ||
| end | ||
|
|
||
| @impl true | ||
|
|
@@ -1214,8 +1214,12 @@ defmodule Ecto.Adapters.SQL do | |
| ## Transactions | ||
|
|
||
| @doc false | ||
| def transaction(adapter_meta, opts, callback) do | ||
| checkout_or_transaction(:transaction, adapter_meta, opts, callback) | ||
| def transaction(adapter_meta, connection, opts, callback) do | ||
| if opts[:read_only] do | ||
| read_only_transaction(adapter_meta, connection, opts, callback) | ||
| else | ||
| checkout_or_transaction(:transaction, adapter_meta, opts, callback) | ||
| end | ||
| end | ||
|
|
||
| @doc false | ||
|
|
@@ -1468,6 +1472,33 @@ defmodule Ecto.Adapters.SQL do | |
|
|
||
| ## Connection helpers | ||
|
|
||
| defp read_only_transaction(adapter_meta, connection, opts, callback) do | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hrm.... what happens if we are already inside a transaction, which is not read-only, and we call this? One option is to detect and raise but that means additional bookkeeping. And because the database themselves are very different when it comes to this functionality, the API is awkward. I am thinking we don't add this functionality as part of the official
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point 🤔, let me adjust to this |
||
| %{pid: pool, telemetry: telemetry, opts: default_opts, log_stacktrace_mfa: log_stacktrace_mfa} = | ||
| adapter_meta | ||
|
|
||
| opts = with_log(telemetry, log_stacktrace_mfa, [], opts ++ default_opts) | ||
|
|
||
| callback = fn transaction_conn -> | ||
| previous_conn = put_conn(pool, transaction_conn) | ||
|
|
||
| try do | ||
| callback.() | ||
| after | ||
| reset_conn(pool, previous_conn) | ||
| end | ||
| end | ||
|
|
||
| if function_exported?(connection, :read_only_transaction, 3) do | ||
| DBConnection.run( | ||
| get_conn_or_pool(pool, adapter_meta), | ||
| &connection.read_only_transaction(&1, opts, callback), | ||
| opts | ||
| ) | ||
| else | ||
| raise ArgumentError, "read-only transactions are not supported by #{inspect(connection)}" | ||
| end | ||
| end | ||
|
|
||
| defp checkout_or_transaction(fun, adapter_meta, opts, callback) do | ||
| %{pid: pool, telemetry: telemetry, opts: default_opts, log_stacktrace_mfa: log_stacktrace_mfa} = | ||
| adapter_meta | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.