Sha256: 7fab040be544a30bd3699f3db78a8bbf50a8596e29e2d71e32129f2608a6c082

Contents?: true

Size: 1015 Bytes

Versions: 5

Compression:

Stored size: 1015 Bytes

Contents

module DbMod
  # Module which provides transaction blocks for db_mod
  # enabled classes.
  module Transaction
    protected

    # Create a transaction on the db_mod database connection.
    # Calls +BEGIN+ then yields to the given block. Calls
    # +COMMIT+ once the block yields, or +ROLLBACK+ if the
    # block raises an exception.
    #
    # Not thread safe. May not be called from inside another
    # transaction.
    # @return [Object] the result of +yield+
    def transaction
      start_transaction!

      result = yield

      query 'COMMIT'

      result
    rescue
      query 'ROLLBACK'
      raise

    ensure
      end_transaction!
    end

    private

    # Start the database transaction, or fail if
    # one is already open.
    def start_transaction!
      fail DbMod::Exceptions::AlreadyInTransaction if @in_transaction
      @in_transaction = true

      query 'BEGIN'
    end

    # End the database transaction
    def end_transaction!
      @in_transaction = false
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
db_mod-0.0.5 lib/db_mod/transaction.rb
db_mod-0.0.4 lib/db_mod/transaction.rb
db_mod-0.0.3 lib/db_mod/transaction.rb
db_mod-0.0.2 lib/db_mod/transaction.rb
db_mod-0.0.1 lib/db_mod/transaction.rb