1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Update documentation to explain the behavior of explicitly raising ActiveRecord::Rollback in a nested transaction [skip ci]

Closes https://github.com/rails/rails/issues/36229.

Reference: [ActiveRecord::Transactions](6394f9da69/activerecord/lib/active_record/transactions.rb (L135-L174)).

This can be updated in master as well as all the previous applicable releases.
This commit is contained in:
Vishal Telangre 2019-05-11 10:19:32 +05:30
parent 669b52d296
commit a37e9804df
No known key found for this signature in database
GPG key ID: 16BA3EBD701E48D8

View file

@ -198,13 +198,30 @@ module ActiveRecord
#
# == Nested transactions support
#
# #transaction calls can be nested. By default, this makes all database
# statements in the nested transaction block become part of the parent
# transaction. For example, the following behavior may be surprising:
#
# ActiveRecord::Base.transaction do
# Post.create(title: 'first')
# ActiveRecord::Base.transaction do
# Post.create(title: 'second')
# raise ActiveRecord::Rollback
# end
# end
#
# This creates both "first" and "second" posts. Reason is the
# ActiveRecord::Rollback exception in the nested block does not issue a
# ROLLBACK. Since these exceptions are captured in transaction blocks,
# the parent block does not see it and the real transaction is committed.
#
# Most databases don't support true nested transactions. At the time of
# writing, the only database that supports true nested transactions that
# we're aware of, is MS-SQL.
#
# In order to get around this problem, #transaction will emulate the effect
# of nested transactions, by using savepoints:
# https://dev.mysql.com/doc/refman/5.7/en/savepoint.html
# https://dev.mysql.com/doc/refman/5.7/en/savepoint.html.
# Savepoints are supported by MySQL and PostgreSQL. SQLite3 version >= '3.6.8'
# supports savepoints.
#
@ -218,6 +235,26 @@ module ActiveRecord
# - However, if +:requires_new+ is set, the block will be wrapped in a
# database savepoint acting as a sub-transaction.
#
# In order to get a ROLLBACK for the nested transaction you may ask for a
# real sub-transaction by passing <tt>requires_new: true</tt>.
# If anything goes wrong, the database rolls back to the beginning of
# the sub-transaction without rolling back the parent transaction.
# If we add it to the previous example:
#
# ActiveRecord::Base.transaction do
# Post.create(title: 'first')
# ActiveRecord::Base.transaction(requires_new: true) do
# Post.create(title: 'second')
# raise ActiveRecord::Rollback
# end
# end
#
# only post with title "first" is created.
# This works on MySQL and PostgreSQL. SQLite3 version >= '3.6.8' also
# supports it.
#
# See ActiveRecord::Transactions to learn more.
#
# === Caveats
#
# MySQL doesn't support DDL transactions. If you perform a DDL operation,