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


160
161
162
# File 'lib/neo4j/transaction.rb', line 160

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

#new(session = Session.current!) ⇒ 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]


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
# File 'lib/neo4j/transaction.rb', line 147

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

  if args.size == 1
    fail ArgumentError, 'Session must be specified' if !args[0].is_a?(Neo4j::Core::CypherSession)

    [args[0], true]
  else
    [true, false].include?(args[0]) ? args.reverse : args.dup
  end
end

#stack_for(session) ⇒ Object


164
165
166
167
# File 'lib/neo4j/transaction.rb', line 164

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