This blog is part of our Rails 6.1 series.
Rails 6.1 deprecates the use of return, break or throw to exit a
transaction block.
>> Post.transaction do
>> @post.update(post_params)
>>
>> break # or return
>> end
# => TRANSACTION (0.1ms) begin transaction
# => DEPRECATION WARNING: Using `return`, `break` or `throw` to exit a transaction block is
# => deprecated without replacement. If the `throw` came from
# => `Timeout.timeout(duration)`, pass an exception class as a second
# => argument so it doesn't use `throw` to abort its block. This results
# => in the transaction being committed, but in the next release of Rails
# => it will rollback.
# => TRANSACTION (0.8ms) commit transaction
>> Timeout.timeout(1) do
>> Post.transaction do
>> @post.update(post_params)
>>
>> sleep 3 # simulate slow request
>> end
>> end
# => TRANSACTION (0.1ms) begin transaction
# => DEPRECATION WARNING: Using `return`, `break` or `throw` to exit a transaction block is
# => deprecated without replacement. If the `throw` came from
# => `Timeout.timeout(duration)`, pass an exception class as a second
# => argument so it doesn't use `throw` to abort its block. This results
# => in the transaction being committed, but in the next release of Rails
# => it will rollback.
# => TRANSACTION (1.6ms) commit transaction
# => Completed 500 Internal Server Error in 1022ms (ActiveRecord: 3.2ms | Allocations: 9736)
# => Timeout::Error (execution expired)
Here, even when the error was thrown the transaction is committed. This is something which is going to change in the future versions.
This is done because currently, when a transaction block is wrapped in
Timeout.timeout(duration) i.e. without second argument(an exception class)
then it uses throw to exit the transaction.
>> Timeout.timeout(1, Timeout::Error) do
>> Post.transaction do
>> @post.update(post_params)
>>
>> sleep 3 # simulate slow request
>> end
>> end
# => TRANSACTION (0.1ms) begin transaction
# => TRANSACTION (0.7ms) rollback transaction
# => Timeout::Error (execution expired)
Check out the pull request for more details on this.

Written bySandip Mane
Related blogs
- 01Rails 6.1 adds invert_where methodMay 4, 2021 · Chimed Palden
- 02Rails 6.1 adds in: option to validate numeric rangesApril 14, 2021 · Akanksha Jain
- 03Rails 6.1 adds delegated_type to ActiveRecordApril 6, 2021 · Akhil Gautam
- 04Rails 6.1 adds nulls_first and nulls_last to ArelMarch 9, 2021 · Berin Larson