View Source Memento.Transaction (memento v0.4.1)

Memento's wrapper around Mnesia transactions. This module exports methods execute/2 and execute_sync/2, and their bang versions, which accept a function to be executed and an optional argument to set the maximum no. of retries until the transaction succeeds.

execute/1 and execute!/1 can be directly called from the base Memento module as the alias Memento.transaction/1 and Memento.transaction!/1 methods, but if you want to specify a custom retries value or use the synchronous version, you should use the methods in this module.

Examples

# Read a User record
{:ok, user} =
  Memento.transaction fn ->
    Memento.Query.read(User, id)
  end

# Get all Users, raising errors on aborts
users =
  Memento.transaction! fn ->
    Memento.Query.all(User)
  end

# Update a User record on all nodes synchronously,
# with a maximum of 5 retries
operation = fn ->
  Memento.Query.write(%User{id: 3, name: "New Value"})
end
Memento.Transaction.execute_sync(operation, 5)

Summary

Types

Maximum no. of retries for a transaction

Functions

Aborts a Memento transaction.

Execute passed function as part of an Mnesia transaction.

Same as execute/2 but returns the result or raises an error.

Execute the transaction in synchronization with all nodes.

Same as execute_sync/2 but returns the result or raises an error.

Checks if you are inside a transaction.

Types

@type retries() :: :infinity | non_neg_integer()

Maximum no. of retries for a transaction

Functions

Link to this function

abort(reason \\ :no_reason_given)

View Source
@spec abort(term()) :: no_return()

Aborts a Memento transaction.

Causes the transaction to return an error tuple with the passed argument: {:error, {:transaction_aborted, reason}}. Outside the context of a transaction, simply raises an error.

In the bang versions of the transactions, it raises a Memento.TransactionAborted error instead of returning the error tuple. Default value for reason is :no_reason_given.

Link to this function

execute(function, retries \\ :infinity)

View Source
@spec execute((... -> any()), retries()) :: {:ok, any()} | {:error, any()}

Execute passed function as part of an Mnesia transaction.

Default value of retries is :infinity. Returns either {:ok, result} or {:error, reason}. Also see :mnesia.transaction/2.

Link to this function

execute!(fun, retries \\ :infinity)

View Source
@spec execute!((... -> any()), retries()) :: any() | no_return()

Same as execute/2 but returns the result or raises an error.

Link to this function

execute_sync(function, retries \\ :infinity)

View Source
@spec execute_sync((... -> any()), retries()) :: {:ok, any()} | {:error, any()}

Execute the transaction in synchronization with all nodes.

This method waits until the data has been committed and logged to disk (if used) on all involved nodes before it finishes. This is useful to ensure that a transaction process does not overload the databases on other nodes.

Returns either {:ok, result} or {:error, reason}. Also see :mnesia.sync_transaction/2.

Link to this function

execute_sync!(fun, retries \\ :infinity)

View Source
@spec execute_sync!((... -> any()), retries()) :: any() | no_return()

Same as execute_sync/2 but returns the result or raises an error.

@spec inside?() :: boolean()

Checks if you are inside a transaction.