Move transaction joinability into the transaction object

This commit is contained in:
Jon Leighton 2012-09-14 19:06:02 +01:00
parent a6fbddb7be
commit 280587588a
5 changed files with 36 additions and 20 deletions

@ -3,8 +3,7 @@ module ConnectionAdapters # :nodoc:
module DatabaseStatements
def initialize
super
@transaction_joinable = nil
@transaction = ClosedTransaction.new(self)
@transaction = ClosedTransaction.new(self)
end
# Converts an arel AST to SQL
@ -173,14 +172,11 @@ def supports_statement_cache?
def transaction(options = {})
options.assert_valid_keys :requires_new, :joinable
last_transaction_joinable = @transaction_joinable
@transaction_joinable = options.fetch(:joinable, true)
requires_new = options[:requires_new] || !last_transaction_joinable
transaction_open = false
transaction_open = false
begin
if @transaction.closed? || requires_new
begin_transaction
if options[:requires_new] || !current_transaction.joinable?
begin_transaction(options)
transaction_open = true
end
@ -195,11 +191,9 @@ def transaction(options = {})
end
ensure
@transaction_joinable = last_transaction_joinable
if outside_transaction?
@transaction = ClosedTransaction.new(self)
elsif @transaction.open? && transaction_open
elsif current_transaction.open? && transaction_open
begin
commit_transaction
rescue Exception
@ -209,12 +203,18 @@ def transaction(options = {})
end
end
def transaction_state #:nodoc:
def current_transaction #:nodoc:
@transaction
end
def begin_transaction #:nodoc:
def transaction_open?
@transaction.open?
end
def begin_transaction(options = {}) #:nodoc:
@transaction = @transaction.begin
@transaction.joinable = options.fetch(:joinable, true)
@transaction
end
def commit_transaction #:nodoc:

@ -25,13 +25,20 @@ def open?
false
end
def joinable?
false
end
# This is a noop when there are no open transactions
def add_record(record)
end
end
class OpenTransaction < Transaction #:nodoc:
attr_reader :parent, :records
attr_reader :parent, :records
attr_accessor :joinable
alias joinable? joinable
def initialize(connection, parent)
super connection

@ -247,15 +247,16 @@ def open_transactions
end
def increment_open_transactions
ActiveSupport::Deprecation.warn "increment_open_transactions is deprecated and has no effect"
ActiveSupport::Deprecation.warn "#increment_open_transactions is deprecated and has no effect"
end
def decrement_open_transactions
ActiveSupport::Deprecation.warn "decrement_open_transactions is deprecated and has no effect"
ActiveSupport::Deprecation.warn "#decrement_open_transactions is deprecated and has no effect"
end
def transaction_joinable=(joinable)
@transaction_joinable = joinable
ActiveSupport::Deprecation.warn "#transaction_joinable= is deprecated. Please pass the :joinable option to #begin_transaction instead."
@transaction.joinable = joinable
end
def create_savepoint

@ -843,8 +843,7 @@ def setup_fixtures
end
@fixture_connections = enlist_fixture_connections
@fixture_connections.each do |connection|
connection.begin_transaction
connection.transaction_joinable = false
connection.begin_transaction joinable: false
end
# Load fixtures for every test.
else
@ -867,7 +866,7 @@ def teardown_fixtures
# Rollback changes if a transaction is active.
if run_in_transaction?
@fixture_connections.each do |connection|
connection.rollback_transaction if connection.transaction_state.open?
connection.rollback_transaction if connection.transaction_open?
end
@fixture_connections.clear
end

@ -581,5 +581,14 @@ def test_transaction_isolation__read_committed
assert_equal original_salary, Developer.find(1).salary
end
test "#transaction_joinable= is deprecated" do
Developer.transaction do
conn = Developer.connection
assert conn.current_transaction.joinable?
assert_deprecated { conn.transaction_joinable = false }
assert !conn.current_transaction.joinable?
end
end
end
end