aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb31
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/abstract_adapter.rb19
2 files changed, 25 insertions, 25 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
index cecbc6b3ac..39118583bd 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -89,14 +89,8 @@ module ActiveRecord
# - The block will be run without doing anything. All database statements
# that happen within the block are effectively appended to the already
# open database transaction.
- # - However, if +start_db_transaction+ is set to true, then the block will
- # be run inside a new database savepoint, effectively making the block
- # a sub-transaction.
- # - If the #transactional_fixtures attribute is set to true, then the first
- # nested call to #transaction will create a new savepoint instead of
- # doing nothing. This makes it possible for toplevel transactions in unit
- # tests to behave like real transactions, even though a database
- # transaction has already been opened.
+ # - However, if +requires_new+ is set, the block will be wrapped in a
+ # database savepoint acting as a sub-transaction.
#
# === Caveats
#
@@ -111,20 +105,25 @@ module ActiveRecord
# already-automatically-released savepoints:
#
# Model.connection.transaction do # BEGIN
- # Model.connection.transaction(true) do # CREATE SAVEPOINT rails_savepoint_1
+ # Model.connection.transaction(:requires_new => true) do # CREATE SAVEPOINT active_record_1
# Model.connection.create_table(...)
- # # rails_savepoint_1 now automatically released
- # end # RELEASE savepoint rails_savepoint_1 <--- BOOM! database error!
+ # # active_record_1 now automatically released
+ # end # RELEASE SAVEPOINT active_record_1 <--- BOOM! database error!
# end
- def transaction(start_db_transaction = false)
- start_db_transaction ||= open_transactions == 0 || (open_transactions == 1 && transactional_fixtures)
+ def transaction(options = {})
+ options.assert_valid_keys :requires_new, :joinable
+
+ last_transaction_joinable, @transaction_joinable =
+ @transaction_joinable, options[:joinable] || true
+ requires_new = options[:requires_new] || !last_transaction_joinable
+
transaction_open = false
begin
if block_given?
- if start_db_transaction
+ if requires_new || open_transactions == 0
if open_transactions == 0
begin_db_transaction
- else
+ elsif requires_new
create_savepoint
end
increment_open_transactions
@@ -145,6 +144,8 @@ module ActiveRecord
raise unless database_transaction_rollback.is_a? ActiveRecord::Rollback
end
ensure
+ @transaction_joinable = last_transaction_joinable
+
if outside_transaction?
@open_transactions = 0
elsif transaction_open
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
index 5137b0f78c..a8cd9f033b 100755
--- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -165,24 +165,23 @@ module ActiveRecord
def decrement_open_transactions
@open_transactions -= 1
end
-
+
+ def transaction_joinable=(joinable)
+ @transaction_joinable = joinable
+ end
+
def create_savepoint
end
-
+
def rollback_to_savepoint
end
-
+
def release_savepoint
end
-
+
def current_savepoint_name
- "rails_savepoint_#{open_transactions}"
+ "active_record_#{open_transactions}"
end
-
- # Whether this AbstractAdapter is currently being used inside a unit test
- # with transactional fixtures turned on. See DatabaseStatements#transaction
- # for more information about the effect of this option.
- attr_accessor :transactional_fixtures
def log_info(sql, name, ms)
if @logger && @logger.debug?