Sha256: 22b4f7299a905c09144ad19951dcf38437c8a0b9b77b6e2779bf6cab0024d6a5

Contents?: true

Size: 1.65 KB

Versions: 17

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module ActiveGraph
  module Transactions
    extend ActiveSupport::Concern

    included do
      thread_mattr_accessor :explicit_session, :tx, :last_bookmark
    end

    class_methods do
      def session(**session_config)
        ActiveGraph::Base.driver.session(**session_config) do |session|
          self.explicit_session = session
          yield session
        ensure
          self.last_bookmark = session.last_bookmark
        end
      end

      def write_transaction(**config, &block)
        send_transaction(:write_transaction, **config, &block)
      end

      def read_transaction(**config, &block)
        send_transaction(:read_transaction, **config, &block)
      end

      alias transaction write_transaction

      private

      def send_transaction(method, **config, &block)
        return yield tx if tx&.open?
        return run_transaction_work(explicit_session, method, **config, &block) if explicit_session&.open?
        driver.session do |session|
          run_transaction_work(session, method, **config, &block)
        end
      end

      def run_transaction_work(session, method, **config, &block)
        implicit = config.delete(:implicit)
        session.send(method, **config) do |tx|
          self.tx = tx
          block.call(tx).tap do |result|
            if implicit &&
              [Core::Result, ActiveGraph::Node::Query::QueryProxy, ActiveGraph::Core::Query]
                .any?(&result.method(:is_a?))
              result.store
            end
          end
        end.tap { tx.apply_callbacks }
      rescue ActiveGraph::Rollback
        # rollbacks are silently swallowed
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
activegraph-11.5.0.alpha.1 lib/active_graph/transactions.rb
activegraph-11.4.0 lib/active_graph/transactions.rb
activegraph-11.3.1 lib/active_graph/transactions.rb
activegraph-11.3.0 lib/active_graph/transactions.rb
activegraph-11.2.0 lib/active_graph/transactions.rb
activegraph-11.1.0 lib/active_graph/transactions.rb
activegraph-11.1.0.beta.1 lib/active_graph/transactions.rb
activegraph-11.1.0.alpha.4 lib/active_graph/transactions.rb
activegraph-11.1.0.alpha.3 lib/active_graph/transactions.rb
activegraph-11.1.0.alpha.2 lib/active_graph/transactions.rb
activegraph-11.1.0.alpha.1 lib/active_graph/transactions.rb
activegraph-11.0.2-java lib/active_graph/transactions.rb
activegraph-11.0.1-java lib/active_graph/transactions.rb
activegraph-11.0.0-java lib/active_graph/transactions.rb
activegraph-11.0.0.beta.4-java lib/active_graph/transactions.rb
activegraph-11.0.0.beta.3-java lib/active_graph/transactions.rb
activegraph-11.0.0.beta.2-java lib/active_graph/transactions.rb