Method: DataMapper::Transaction#commit

Defined in:
lib/gems/dm-core-0.9.9/lib/dm-core/transaction.rb

#commitObject

Note:

If no block is given, it will simply commit any changes made since the Transaction did #begin.

Commit the transaction

Parameters:

  • block (Block)

    a block (taking the one argument, the Transaction) to execute within this transaction. The transaction will begin and commit around the block, and roll back if an exception is raised.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/transaction.rb', line 86

def commit
  if block_given?
    raise "Illegal state for commit with block: #{@state}" unless @state == :none
    begin
      self.begin
      rval = within { |*block_args| yield(*block_args) }
      self.commit if @state == :begin
      return rval
    rescue Exception => e
      self.rollback if @state == :begin
      raise e
    end
  else
    raise "Illegal state for commit without block: #{@state}" unless @state == :begin
    each_adapter(:prepare_adapter, [:rollback_and_close_adapter_if_begin, :rollback_prepared_and_close_adapter_if_prepare])
    each_adapter(:commit_adapter, [:log_fatal_transaction_breakage])
    each_adapter(:close_adapter, [:log_fatal_transaction_breakage])
    @state = :commit
  end
end