Method: ROM::SQL::Relation#transaction

Defined in:
lib/rom/sql/relation.rb

#transaction(opts = EMPTY_HASH) {|t| ... } ⇒ Mixed

Open a database transaction

Parameters:

  • opts (Hash) (defaults to: EMPTY_HASH)

Options Hash (opts):

  • :auto_savepoint (Boolean)

    Automatically use a savepoint for Database#transaction calls inside this transaction block.

  • :isolation (Symbol)

    The transaction isolation level to use for this transaction, should be :uncommitted, :committed, :repeatable, or :serializable, used if given and the database/adapter supports customizable transaction isolation levels.

  • :num_retries (Integer)

    The number of times to retry if the :retry_on option is used. The default is 5 times. Can be set to nil to retry indefinitely, but that is not recommended.

  • :before_retry (Proc)

    Proc to execute before rertrying if the :retry_on option is used. Called with two arguments: the number of retry attempts (counting the current one) and the error the last attempt failed with.

  • :prepare (String)

    A string to use as the transaction identifier for a prepared transaction (two-phase commit), if the database/adapter supports prepared transactions.

  • :retry_on (Class)

    An exception class or array of exception classes for which to automatically retry the transaction. Can only be set if not inside an existing transaction. Note that this should not be used unless the entire transaction block is idempotent, as otherwise it can cause non-idempotent behavior to execute multiple times.

  • :rollback (Symbol)

    Can the set to :reraise to reraise any Sequel::Rollback exceptions raised, or :always to always rollback even if no exceptions occur (useful for testing).

  • :server (Symbol)

    The server to use for the transaction. Set to :default, :read_only, or whatever symbol you used in the connect string when naming your servers.

  • :savepoint (Boolean)

    Whether to create a new savepoint for this transaction, only respected if the database/adapter supports savepoints. By default Sequel will reuse an existing transaction, so if you want to use a savepoint you must use this option. If the surrounding transaction uses :auto_savepoint, you can set this to false to not use a savepoint. If the value given for this option is :only, it will only create a savepoint if it is inside a transacation.

  • :deferrable (Boolean)

    **PG 9.1+ only** If present, set to DEFERRABLE if true or NOT DEFERRABLE if false.

  • :read_only (Boolean)

    **PG only** If present, set to READ ONLY if true or READ WRITE if false.

  • :synchronous (Symbol)

    **PG only** if non-nil, set synchronous_commit appropriately. Valid values true, :on, false, :off, :local (9.1+), and :remote_write (9.2+).

Yields:

  • (t)

    Transaction

Returns:

  • (Mixed)


140
141
142
# File 'lib/rom/sql/relation.rb', line 140

def transaction(opts = EMPTY_HASH, &block)
  Transaction.new(dataset.db).run(opts, &block)
end