Sha256: 3ede4f5968ff9418fb5dffcc1479173bb57c285a66841e993b50a6a601551405

Contents?: true

Size: 1.08 KB

Versions: 10

Compression:

Stored size: 1.08 KB

Contents

# private
module Simple::SQL::SimpleTransactions
  def tx_nesting_level
    @tx_nesting_level ||= 0
  end

  def tx_nesting_level=(tx_nesting_level)
    @tx_nesting_level = tx_nesting_level
  end

  def transaction(&_block)
    # Notes: by using "ensure" (as opposed to rescue) we are rolling back
    # both when an exception was raised and when a value was thrown. This
    # also means we have to track whether or not to rollback. i.e. do roll
    # back when we yielded to &block but not otherwise.
    #
    # Also the transaction support is a bit limited: you cannot rollback.
    # Rolling back from inside a nested transaction would require SAVEPOINT
    # support; without the code is simpler at least :)

    if tx_nesting_level == 0
      exec "BEGIN"
      tx_started = true
    end

    self.tx_nesting_level += 1

    return_value = yield

    # Only commit if we started a transaction here.
    if tx_started
      exec "COMMIT"
      tx_committed = true
    end

    return_value
  ensure
    self.tx_nesting_level -= 1
    if tx_started && !tx_committed
      exec "ROLLBACK"
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
simple-sql-0.4.32 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.31 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.30 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.29 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.28 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.27 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.26 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.25 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.24 lib/simple/sql/simple_transactions.rb
simple-sql-0.4.23 lib/simple/sql/simple_transactions.rb