Skip to content

Commit

Permalink
When state is invalidated, skip rollbacks and raise on commits
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiemccarthy authored and rafaelfranca committed Jun 17, 2021
1 parent bc5d5e2 commit 5e4d3a3
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def full_rollback!
end

def commit!
if uncommittable?
raise ActiveRecord::TransactionIsolationError, "cannot set state committed when already #{state.to_s}"
end
@state = :committed
end

Expand Down Expand Up @@ -195,11 +198,14 @@ def materialize!
end

def rollback
connection.rollback_to_savepoint(savepoint_name) if materialized?
connection.rollback_to_savepoint(savepoint_name) if materialized? && !state.invalidated?
@state.rollback!
end

def commit
if state.invalidated?
raise ActiveRecord::StatementInvalid, "cannot commit after transaction invalidated"
end
connection.release_savepoint(savepoint_name) if materialized?
@state.commit!
end
Expand All @@ -219,11 +225,14 @@ def materialize!
end

def rollback
connection.rollback_db_transaction if materialized?
connection.rollback_db_transaction if materialized? && !state.invalidated?
@state.full_rollback!
end

def commit
if state.invalidated?
raise ActiveRecord::StatementInvalid, "cannot commit after transaction invalidated"
end
connection.commit_db_transaction if materialized?
@state.full_commit!
end
Expand Down Expand Up @@ -301,6 +310,9 @@ def materialize_transactions
def commit_transaction
@connection.lock.synchronize do
transaction = @stack.last
if transaction.state.invalidated?
raise ActiveRecord::StatementInvalid, "cannot commit after transaction invalidated"
end

begin
transaction.before_commit_records
Expand All @@ -316,7 +328,7 @@ def commit_transaction
def rollback_transaction(transaction = nil)
@connection.lock.synchronize do
transaction ||= @stack.pop
transaction.rollback
transaction.rollback unless transaction.state.invalidated?
transaction.rollback_records
end
end
Expand All @@ -329,6 +341,7 @@ def within_new_transaction(isolation: nil, joinable: true)
ret
rescue Exception => error
if transaction
transaction.state.invalidate! if error.is_a? ActiveRecord::TransactionRollbackError
rollback_transaction
after_failure_actions(transaction, error)
end
Expand Down

0 comments on commit 5e4d3a3

Please sign in to comment.