mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
allow raise after explicit rollback within #transaction(&block)
This commit is contained in:
parent
dc26498948
commit
ed3ed71501
2 changed files with 21 additions and 10 deletions
|
@ -111,6 +111,26 @@ describe DB::Transaction do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "transaction can be rolledback within block and later raise" do
|
||||||
|
with_dummy_connection do |cnn|
|
||||||
|
expect_raises(FooException) do
|
||||||
|
cnn.transaction do |tx|
|
||||||
|
tx.rollback
|
||||||
|
raise FooException.new
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "transaction can be rolledback within block and later raise DB::Rollback without forwarding it" do
|
||||||
|
with_dummy_connection do |cnn|
|
||||||
|
cnn.transaction do |tx|
|
||||||
|
tx.rollback
|
||||||
|
raise DB::Rollback.new
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "transaction can't be committed twice" do
|
it "transaction can't be committed twice" do
|
||||||
with_dummy_connection do |cnn|
|
with_dummy_connection do |cnn|
|
||||||
cnn.transaction do |tx|
|
cnn.transaction do |tx|
|
||||||
|
|
|
@ -7,16 +7,7 @@ module DB
|
||||||
begin
|
begin
|
||||||
yield tx
|
yield tx
|
||||||
rescue e
|
rescue e
|
||||||
# TODO check if `unless tx.closed?` should be added.
|
tx.rollback unless tx.closed?
|
||||||
# If it is not added, when an exception occurs after the
|
|
||||||
# transaction is closed (explicit rollback/commit)
|
|
||||||
# the call to rollback will trigger an exception.
|
|
||||||
# Since the code will be in between `#transaction(&block)`
|
|
||||||
# seems reasonable to raise an error since it might trigger
|
|
||||||
# a design flaw. Transaction might been commited already.
|
|
||||||
# Maybe we should wrap e in another exception that clarifies
|
|
||||||
# this scenario.
|
|
||||||
tx.rollback
|
|
||||||
raise e unless e.is_a?(DB::Rollback)
|
raise e unless e.is_a?(DB::Rollback)
|
||||||
else
|
else
|
||||||
tx.commit unless tx.closed?
|
tx.commit unless tx.closed?
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue