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:
parent
669b52d296
commit
a37e9804df
1 changed files with 38 additions and 1 deletions
|
@ -198,13 +198,30 @@ module ActiveRecord
|
||||||
#
|
#
|
||||||
# == Nested transactions support
|
# == 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
|
# Most databases don't support true nested transactions. At the time of
|
||||||
# writing, the only database that supports true nested transactions that
|
# writing, the only database that supports true nested transactions that
|
||||||
# we're aware of, is MS-SQL.
|
# we're aware of, is MS-SQL.
|
||||||
#
|
#
|
||||||
# In order to get around this problem, #transaction will emulate the effect
|
# In order to get around this problem, #transaction will emulate the effect
|
||||||
# of nested transactions, by using savepoints:
|
# 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'
|
# Savepoints are supported by MySQL and PostgreSQL. SQLite3 version >= '3.6.8'
|
||||||
# supports savepoints.
|
# supports savepoints.
|
||||||
#
|
#
|
||||||
|
@ -218,6 +235,26 @@ module ActiveRecord
|
||||||
# - However, if +:requires_new+ is set, the block will be wrapped in a
|
# - However, if +:requires_new+ is set, the block will be wrapped in a
|
||||||
# database savepoint acting as a sub-transaction.
|
# 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
|
# === Caveats
|
||||||
#
|
#
|
||||||
# MySQL doesn't support DDL transactions. If you perform a DDL operation,
|
# MySQL doesn't support DDL transactions. If you perform a DDL operation,
|
||||||
|
|
Loading…
Reference in a new issue