Method: Sequel::Database#transaction

Defined in:
lib/sequel/database.rb

#transaction(opts = {}) ⇒ Object

A simple implementation of SQL transactions. Nested transactions are not supported - calling #transaction within a transaction will reuse the current transaction. Should be overridden for databases that support nested transactions.



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/sequel/database.rb', line 455

def transaction(opts={})
  unless opts.is_a?(Hash)
    Deprecation.deprecate('Passing an argument other than a Hash to Database#transaction', "Use DB.transaction(:server=>#{opts.inspect})") 
    opts = {:server=>opts}
  end
  synchronize(opts[:server]) do |conn|
    return yield(conn) if @transactions.include?(Thread.current)
    log_info(begin_transaction_sql)
    conn.execute(begin_transaction_sql)
    begin
      @transactions << Thread.current
      yield(conn)
    rescue Exception => e
      log_info(rollback_transaction_sql)
      conn.execute(rollback_transaction_sql)
      transaction_error(e)
    ensure
      unless e
        log_info(commit_transaction_sql)
        conn.execute(commit_transaction_sql)
      end
      @transactions.delete(Thread.current)
    end
  end
end