Module: Neo4j::Transaction

Extended by:
Transaction
Included in:
Transaction
Defined in:
lib/neo4j/transaction.rb

Defined Under Namespace

Classes: Base, TransactionsRegistry

Instance Method Summary collapse

Instance Method Details

#current_for(session) ⇒ Object



162
163
164
# File 'lib/neo4j/transaction.rb', line 162

def current_for(session)
  stack_for(session).first
end

#new(session = Session.current!) ⇒ Neo4j::Transaction::Instance

Returns:

  • (Neo4j::Transaction::Instance)


120
121
122
# File 'lib/neo4j/transaction.rb', line 120

def new(session = Session.current!)
  session.transaction
end

#run(*args) ⇒ Object

Runs the given block in a new transaction. @@yield [Neo4j::Transaction::Instance]

Parameters:

  • run_in_tx (Boolean)

    if true a new transaction will not be created, instead if will simply yield to the given block



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/neo4j/transaction.rb', line 127

def run(*args)
  session, run_in_tx = session_and_run_in_tx_from_args(args)

  fail ArgumentError, 'Expected a block to run in Transaction.run' unless block_given?

  return yield(nil) unless run_in_tx

  tx = Neo4j::Transaction.new(session)
  yield tx
rescue Exception => e # rubocop:disable Lint/RescueException
  # print_exception_cause(e)

  tx.mark_failed unless tx.nil?
  raise e
ensure
  tx.close unless tx.nil?
end

#session_and_run_in_tx_from_args(args) ⇒ Object

To support old syntax of providing run_in_tx first But session first is ideal



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/neo4j/transaction.rb', line 147

def session_and_run_in_tx_from_args(args)
  fail ArgumentError, 'Too many arguments' if args.size > 2

  if args.empty?
    [Session.current!, true]
  else
    result = args.dup
    if result.size == 1
      result << ([true, false].include?(args[0]) ? Session.current! : true)
    end

    [true, false].include?(result[0]) ? result.reverse : result
  end
end

#stack_for(session) ⇒ Object



166
167
168
169
# File 'lib/neo4j/transaction.rb', line 166

def stack_for(session)
  TransactionsRegistry.transactions_by_session_id ||= {}
  TransactionsRegistry.transactions_by_session_id[session.object_id] ||= []
end