Skip to content

Commit 5135ddc

Browse files
authored
Use retry syntax in Ruby transaction examples (#999)
1 parent d388a11 commit 5135ddc

File tree

1 file changed

+44
-60
lines changed

1 file changed

+44
-60
lines changed

spec/integration/transactions_examples_spec.rb

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,16 @@ def update_employee_info(session)
4040
events_coll.insert_one({ employee: 3, status: { new: 'Inactive', old: 'Active' } },
4141
session: session)
4242

43-
loop do
44-
begin
45-
session.commit_transaction
46-
puts 'Transaction committed.'
47-
break
48-
rescue Mongo::Error => e
49-
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
50-
puts "#{Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL}, retrying commit operation..."
51-
next
52-
else
53-
puts 'Error during commit ...'
54-
raise
55-
end
43+
begin
44+
session.commit_transaction
45+
puts 'Transaction committed.'
46+
rescue Mongo::Error => e
47+
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
48+
puts "#{Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL}, retrying commit operation..."
49+
retry
50+
else
51+
puts 'Error during commit ...'
52+
raise
5653
end
5754
end
5855
end
@@ -78,18 +75,15 @@ def update_employee_info(session)
7875
# Start Transactions Retry Example 1
7976

8077
def run_transaction_with_retry(session)
81-
loop do
82-
begin
83-
yield session # performs transaction
84-
break
85-
rescue Mongo::Error => e
78+
begin
79+
yield session # performs transaction
80+
rescue Mongo::Error => e
8681

87-
puts 'Transaction aborted. Caught exception during transaction.'
88-
raise unless e.label?(Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL)
82+
puts 'Transaction aborted. Caught exception during transaction.'
83+
raise unless e.label?(Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL)
8984

90-
puts "#{Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL}, retrying transaction ..."
91-
next
92-
end
85+
puts "#{Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL}, retrying transaction ..."
86+
retry
9387
end
9488
end
9589

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

114108
def commit_with_retry(session)
115-
loop do
116-
begin
117-
session.commit_transaction
118-
puts 'Transaction committed.'
119-
break
120-
rescue Mongo::Error=> e
121-
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
122-
puts "#{Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL}, retrying commit operation..."
123-
next
124-
else
125-
puts 'Error during commit ...'
126-
raise
127-
end
109+
begin
110+
session.commit_transaction
111+
puts 'Transaction committed.'
112+
rescue Mongo::Error=> e
113+
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
114+
puts "#{Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL}, retrying commit operation..."
115+
retry
116+
else
117+
puts 'Error during commit ...'
118+
raise
128119
end
129120
end
130121
end
@@ -159,34 +150,27 @@ def commit_with_retry(session)
159150
# Start Transactions Retry Example 3
160151

161152
def run_transaction_with_retry(session)
162-
loop do
163-
begin
164-
yield session # performs transaction
165-
break
166-
rescue Mongo::Error=> e
167-
puts 'Transaction aborted. Caught exception during transaction.'
168-
raise unless e.label?(Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL)
169-
170-
puts "#{Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL}, retrying transaction ..."
171-
next
172-
end
153+
begin
154+
yield session # performs transaction
155+
rescue Mongo::Error => e
156+
puts 'Transaction aborted. Caught exception during transaction.'
157+
raise unless e.label?(Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL)
158+
puts "#{Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL}, retrying transaction ..."
159+
retry
173160
end
174161
end
175162

176163
def commit_with_retry(session)
177-
loop do
178-
begin
179-
session.commit_transaction
180-
puts 'Transaction committed.'
181-
break
182-
rescue Mongo::Error => e
183-
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
184-
puts "#{Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL}, retrying commit operation ..."
185-
next
186-
else
187-
puts 'Error during commit ...'
188-
raise
189-
end
164+
begin
165+
session.commit_transaction
166+
puts 'Transaction committed.'
167+
rescue Mongo::Error => e
168+
if e.label?(Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)
169+
puts "#{Mongo::Error::UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL}, retrying commit operation ..."
170+
retry
171+
else
172+
puts 'Error during commit ...'
173+
raise
190174
end
191175
end
192176
end

0 commit comments

Comments
 (0)