Skip to content

Commit db46204

Browse files
committed
when "pop-ing" current savepoint name - consider open transaction count
- this allows us to work with user marked savepoints as intended, since they will likely execute within a single AR transaction object - but at the same time AR's nested transaction emulation (using savepoints) will work in a Rails compatible way (fixes #477)
1 parent 0267224 commit db46204

File tree

3 files changed

+56
-48
lines changed

3 files changed

+56
-48
lines changed

lib/arjdbc/jdbc/adapter.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,15 @@ def release_savepoint(name = current_savepoint_name)
402402
# @since 1.3.0
403403
# @override
404404
def current_savepoint_name(create = nil)
405-
return "active_record_#{open_transactions}" if create
406-
@connection.marked_savepoint_names.last || "active_record_#{open_transactions}"
405+
open_tx = open_transactions
406+
return "active_record_#{open_tx}" if create
407+
408+
sp_names = @connection.marked_savepoint_names
409+
unless sp_names.empty?
410+
sp_names[ -(sp_names.size - open_tx + 1) ]
411+
else
412+
"active_record_#{open_tx}"
413+
end
407414
end
408415

409416
# Executes a SQL query in the context of this connection using the bind

test/db/sqlite3/transaction_test.rb

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -44,49 +44,4 @@ def test_supports_savepoints
4444
assert_true ActiveRecord::Base.connection.supports_savepoints?
4545
end
4646

47-
def test_many_savepoints
48-
@first = Entry.create
49-
Entry.transaction do
50-
@first.content = "One"
51-
@first.save!
52-
53-
begin
54-
Entry.transaction :requires_new => true do
55-
@first.content = "Two"
56-
@first.save!
57-
58-
begin
59-
Entry.transaction :requires_new => true do
60-
@first.content = "Three"
61-
@first.save!
62-
63-
begin
64-
Entry.transaction :requires_new => true do
65-
@first.content = "Four"
66-
@first.save!
67-
raise
68-
end
69-
rescue
70-
end
71-
72-
@three = @first.reload.content
73-
raise
74-
end
75-
rescue
76-
end
77-
78-
@two = @first.reload.content
79-
raise
80-
end
81-
rescue
82-
end
83-
84-
@one = @first.reload.content
85-
end
86-
87-
assert_equal "One", @one
88-
assert_equal "Two", @two
89-
assert_equal "Three", @three
90-
end
91-
9247
end

test/transaction.rb

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,52 @@ def test_transaction_isolation_repeatable_read
107107
# end
108108
# end if Test::Unit::TestCase.ar_version('4.0')
109109

110+
def test_transaction_nesting
111+
@first = Entry.create
112+
113+
Entry.transaction do
114+
@first.content = "One"
115+
@first.save!
116+
117+
begin
118+
Entry.transaction :requires_new => true do
119+
@first.content = "Two"
120+
@first.save!
121+
122+
begin
123+
Entry.transaction :requires_new => true do
124+
@first.content = "Three"
125+
@first.save!
126+
127+
begin
128+
Entry.transaction :requires_new => true do
129+
@first.content = "Four"
130+
@first.save!
131+
raise
132+
end
133+
rescue
134+
end
135+
136+
@three = @first.reload.content
137+
raise
138+
end
139+
rescue
140+
end
141+
142+
@two = @first.reload.content
143+
raise
144+
end
145+
rescue
146+
end
147+
148+
@one = @first.reload.content
149+
end
150+
151+
assert_equal "One", @one
152+
assert_equal "Two", @two
153+
assert_equal "Three", @three
154+
end
155+
110156
def test_savepoint
111157
omit 'savepoins not supported' unless @supports_savepoints
112158
Entry.create! :title => '1'
@@ -175,7 +221,7 @@ def test_named_savepoint
175221

176222
assert_equal 4, Entry.count
177223

178-
connection.rollback_to_savepoint
224+
connection.rollback_to_savepoint 'SP2'
179225
assert_equal 3, Entry.count
180226

181227
connection.create_savepoint 'SP3'

0 commit comments

Comments
 (0)