method

within_new_transaction

Importance_0
v6.1.7.7 - Show latest stable - 0 notes - Class: TransactionManager
within_new_transaction(isolation: nil, joinable: true) public

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/connection_adapters/abstract/transaction.rb, line 316
      def within_new_transaction(isolation: nil, joinable: true)
        @connection.lock.synchronize do
          transaction = begin_transaction(isolation: isolation, joinable: joinable)
          ret = yield
          completed = true
          ret
        rescue Exception => error
          if transaction
            transaction.state.invalidate! if error.is_a? ActiveRecord::TransactionRollbackError
            rollback_transaction
            after_failure_actions(transaction, error)
          end

          raise
        ensure
          if transaction
            if error
              # @connection still holds an open or invalid transaction, so we must not
              # put it back in the pool for reuse.
              @connection.throw_away! unless transaction.state.rolledback?
            else
              if Thread.current.status == "aborting"
                rollback_transaction
              else
                if !completed && transaction.written
                  ActiveSupport::Deprecation.warn(<<~EOW)
                    Using `return`, `break` or `throw` to exit a transaction block is
                    deprecated without replacement. If the `throw` came from
                    `Timeout.timeout(duration)`, pass an exception class as a second
                    argument so it doesn't use `throw` to abort its block. This results
                    in the transaction being committed, but in the next release of Rails
                    it will rollback.
                  EOW
                end
                begin
                  commit_transaction
                rescue Exception
                  rollback_transaction(transaction) unless transaction.state.completed?
                  raise
                end
              end
            end
          end
        end
      end

      def open_transactions
        @stack.size
      end

      def current_transaction
        @stack.last || NULL_TRANSACTION
      end

      private
        NULL_TRANSACTION = NullTransaction.new

        # Deallocate invalidated prepared statements outside of the transaction
        def after_failure_actions(transaction, error)
          return unless transaction.is_a?(RealTransaction)
          return unless error.is_a?(ActiveRecord::PreparedStatementCacheExpired)
          @connection.clear_cache!
        end
    end
  end
end
Register or log in to add new notes.