Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 44 additions & 60 deletions spec/integration/transactions_examples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,16 @@ def update_employee_info(session)
events_coll.insert_one({ employee: 3, status: { new: 'Inactive', old: 'Active' } },
session: session)

loop do
begin
session.commit_transaction
puts 'Transaction committed.'
break
rescue Mongo::Error => e
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
puts "#{Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL}, retrying commit operation..."
next
else
puts 'Error during commit ...'
raise
end
begin
session.commit_transaction
puts 'Transaction committed.'
rescue Mongo::Error => e
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
puts "#{Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL}, retrying commit operation..."
retry
else
puts 'Error during commit ...'
raise
end
end
end
Expand All @@ -78,18 +75,15 @@ def update_employee_info(session)
# Start Transactions Retry Example 1

def run_transaction_with_retry(session)
loop do
begin
yield session # performs transaction
break
rescue Mongo::Error => e
begin
yield session # performs transaction
rescue Mongo::Error => e

puts 'Transaction aborted. Caught exception during transaction.'
raise unless e.label?(Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL)
puts 'Transaction aborted. Caught exception during transaction.'
raise unless e.label?(Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL)

puts "#{Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL}, retrying transaction ..."
next
end
puts "#{Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL}, retrying transaction ..."
retry
end
end

Expand All @@ -112,19 +106,16 @@ def run_transaction_with_retry(session)
# Start Transactions Retry Example 2

def commit_with_retry(session)
loop do
begin
session.commit_transaction
puts 'Transaction committed.'
break
rescue Mongo::Error=> e
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
puts "#{Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL}, retrying commit operation..."
next
else
puts 'Error during commit ...'
raise
end
begin
session.commit_transaction
puts 'Transaction committed.'
rescue Mongo::Error=> e
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
puts "#{Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL}, retrying commit operation..."
retry
else
puts 'Error during commit ...'
raise
end
end
end
Expand Down Expand Up @@ -159,34 +150,27 @@ def commit_with_retry(session)
# Start Transactions Retry Example 3

def run_transaction_with_retry(session)
loop do
begin
yield session # performs transaction
break
rescue Mongo::Error=> e
puts 'Transaction aborted. Caught exception during transaction.'
raise unless e.label?(Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL)

puts "#{Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL}, retrying transaction ..."
next
end
begin
yield session # performs transaction
rescue Mongo::Error => e
puts 'Transaction aborted. Caught exception during transaction.'
raise unless e.label?(Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL)
puts "#{Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL}, retrying transaction ..."
retry
end
end

def commit_with_retry(session)
loop do
begin
session.commit_transaction
puts 'Transaction committed.'
break
rescue Mongo::Error => e
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
puts "#{Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL}, retrying commit operation ..."
next
else
puts 'Error during commit ...'
raise
end
begin
session.commit_transaction
puts 'Transaction committed.'
rescue Mongo::Error => e
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
puts "#{Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL}, retrying commit operation ..."
retry
else
puts 'Error during commit ...'
raise
end
end
end
Expand Down