Skip to content

Commit 46339b2

Browse files
committed
Revert "Merge pull request rails#44531 from matthewd/rm-mark-written"
This reverts commit 199fe99, reversing changes made to d49f956.
1 parent 13dd6f9 commit 46339b2

File tree

7 files changed

+18
-1
lines changed

7 files changed

+18
-1
lines changed

activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ def transaction(requires_new: nil, isolation: nil, joinable: true, &block)
326326
:disable_lazy_transactions!, :enable_lazy_transactions!, :dirty_current_transaction,
327327
to: :transaction_manager
328328

329+
def mark_transaction_written_if_write(sql) # :nodoc:
330+
transaction = current_transaction
331+
if transaction.open?
332+
transaction.written ||= write_query?(sql)
333+
end
334+
end
335+
329336
def transaction_open?
330337
current_transaction.open?
331338
end

activerecord/lib/active_record/connection_adapters/abstract/transaction.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def dirty!; end
8787

8888
class Transaction # :nodoc:
8989
attr_reader :connection, :state, :savepoint_name, :isolation_level
90+
attr_accessor :written
9091

9192
def initialize(connection, isolation: nil, joinable: true, run_commit_callbacks: false)
9293
@connection = connection
@@ -419,7 +420,7 @@ def within_new_transaction(isolation: nil, joinable: true)
419420
# @connection still holds an open or invalid transaction, so we must not
420421
# put it back in the pool for reuse.
421422
@connection.throw_away! unless transaction.state.rolledback?
422-
elsif Thread.current.status == "aborting" || !completed
423+
elsif Thread.current.status == "aborting" || (!completed && transaction.written)
423424
# The transaction is still open but the block returned earlier.
424425
#
425426
# The block could return early because of a timeout or because the thread is aborting,

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ def extended_type_map_key
644644

645645
def raw_execute(sql, name, async: false)
646646
materialize_transactions
647+
mark_transaction_written_if_write(sql)
647648

648649
log(sql, name, async: async) do
649650
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do

activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def exec_stmt_and_free(sql, name, binds, cache_stmt: false, async: false)
168168
check_if_write_query(sql)
169169

170170
materialize_transactions
171+
mark_transaction_written_if_write(sql)
171172

172173
# make sure we carry over any changes to ActiveRecord.default_timezone that have been
173174
# made since we established the connection

activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def explain(arel, binds = [])
1212
# Queries the database and returns the results in an Array-like object
1313
def query(sql, name = nil) # :nodoc:
1414
materialize_transactions
15+
mark_transaction_written_if_write(sql)
1516

1617
log(sql, name) do
1718
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
@@ -40,6 +41,7 @@ def execute(sql, name = nil)
4041
check_if_write_query(sql)
4142

4243
materialize_transactions
44+
mark_transaction_written_if_write(sql)
4345

4446
log(sql, name) do
4547
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ def execute_and_clear(sql, name, binds, prepare: false, async: false)
760760

761761
def exec_no_cache(sql, name, binds, async: false)
762762
materialize_transactions
763+
mark_transaction_written_if_write(sql)
763764

764765
# make sure we carry over any changes to ActiveRecord.default_timezone that have been
765766
# made since we established the connection
@@ -775,6 +776,7 @@ def exec_no_cache(sql, name, binds, async: false)
775776

776777
def exec_cache(sql, name, binds, async: false)
777778
materialize_transactions
779+
mark_transaction_written_if_write(sql)
778780
update_typemap_for_default_timezone
779781

780782
stmt_key = prepare_statement(sql, binds)

activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def execute(sql, name = nil) # :nodoc:
2525
check_if_write_query(sql)
2626

2727
materialize_transactions
28+
mark_transaction_written_if_write(sql)
2829

2930
log(sql, name) do
3031
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
@@ -38,6 +39,7 @@ def exec_query(sql, name = nil, binds = [], prepare: false, async: false) # :nod
3839
check_if_write_query(sql)
3940

4041
materialize_transactions
42+
mark_transaction_written_if_write(sql)
4143

4244
type_casted_binds = type_casted_binds(binds)
4345

@@ -121,6 +123,7 @@ def execute_batch(statements, name = nil)
121123
check_if_write_query(sql)
122124

123125
materialize_transactions
126+
mark_transaction_written_if_write(sql)
124127

125128
log(sql, name) do
126129
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do

0 commit comments

Comments
 (0)