Method: Sequel::Database#rollback_on_exit

Defined in:
lib/sequel/database/transactions.rb

#rollback_on_exit(opts = OPTS) ⇒ Object

When exiting the transaction block through methods other than an exception (e.g. normal exit, non-local return, or throw), set the current transaction to rollback instead of committing. This is designed for use in cases where you want to preform a non-local return but also want to rollback instead of committing. Options:

:cancel

Cancel the current rollback_on_exit setting, so exiting will commit instead of rolling back.

:savepoint

Rollback only the current savepoint if inside a savepoint. Can also be an positive integer value to rollback that number of enclosing savepoints, up to and including the transaction itself. If the database does not support savepoints, this option is ignored and the entire transaction is affected.

:server

The server/shard the transaction is being executed on.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sequel/database/transactions.rb', line 83

def rollback_on_exit(opts=OPTS)
  synchronize(opts[:server]) do |conn|
    raise Error, "Cannot call Sequel:: Database#rollback_on_exit unless inside a transaction" unless h = _trans(conn)
    rollback = !opts[:cancel]

    if supports_savepoints?
      savepoints = h[:savepoints]

      if level = opts[:savepoint]
        level = 1 if level == true
        raise Error, "invalid :savepoint option to Database#rollback_on_exit: #{level.inspect}" unless level.is_a?(Integer)
        raise Error, "cannot pass nonpositive integer (#{level.inspect}) as :savepoint option to Database#rollback_on_exit" if level < 1
        level.times do |i|
          break unless savepoint = savepoints[-1 - i]
          savepoint[:rollback_on_exit] = rollback
        end
      else
        savepoints[0][:rollback_on_exit] = rollback
      end
    else
      h[:rollback_on_exit] = rollback
    end
  end

  nil
end