29
.
04
.
2024
26
.
09
.
2022
Ruby on Rails
Backend
Ruby
Tutorial

How to rescue a transaction to roll back changes?

Paweł Strzałkowski
Chief Technology Officer

The documentation is clear and the answer seems to be obvious. However, as I'm discussing this matter quite frequently, it turns out that some people need a small clarification. I have decided that today is the last day when I explain this issue to anyone. From now on, I will be sending a link to this article and feel exempted from further explanations.

Rescue which does not work as you wish

The following example does not roll back your post object.


ActiveRecord::Base.transaction do
  post.update!(title: "cow")
  post.update!(content: "beer")

  raise StandardError, "cow and beer are not compatible"
rescue StandardError => e
  Rails.logger.error(e.message)
end

The error is rescued and handled. It is not propagated beyond the rescue block. As a result, it doesn't affect the transaction block.

Example

As a quick example, let's create a post with a title and some content. In a transaction block, it is updated twice and then an error is raised. The error is rescued and you can clearly see a COMMIT statement in the SQL log. You can also read the updated values from the post object.

If you intended it to roll back the transaction - it just does not work like that.


> post = Post.create(title: 'bee', content: 'honey')

TRANSACTION (0.1ms)  BEGIN
Post Create (0.6ms)  INSERT INTO "posts"("title", "content", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["title", "bee"], ["content", "honey"], ["created_at", "2022-09-19 13:52:12.518986"], ["updated_at", "2022-09-19 13:52:12.518986"]]
TRANSACTION (2.0ms)  COMMIT
 => #<Post:0x0000000112f4ea30 id: 3, title: "bee", content: "honey", created_at: Mon, 19 Sep 2022 13:52:12.518986000 UTC +00:00, updated_at: Mon, 19 Sep 2022 13:52:12.518986000 UTC +00:00>

> ActiveRecord::Base.transaction do
>   post.update!(title: "cow")
>   post.update!(content: "beer")
>
>   raise StandardError, "cow and beer are not compatible"
> rescue StandardError => e
>   Rails.logger.error(e.message)
> end

TRANSACTION (0.1ms)  BEGIN
  Post Update (0.3ms)  UPDATE "posts" SET "title" = $1, "updated_at" = $2 WHERE "posts"."id" = $3  [["title", "cow"], ["updated_at", "2022-09-19 13:52:17.956795"], ["id", 3]]
  Post Update (0.2ms)  UPDATE "posts" SET "content" = $1, "updated_at" = $2 WHERE "posts"."id" = $3  [["content", "beer"], ["updated_at", "2022-09-19 13:52:17.958060"], ["id", 3]]
cow and beer are not compatible
  TRANSACTION (2.0ms)  COMMIT
 => 32

> post.reload
  Post Load (0.3ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
 => #<Post:0x0000000112f4ea30 id: 3, title: "cow", content: "beer", created_at: Mon, 19 Sep 2022 13:52:12.518986000 UTC +00:00, updated_at: Mon, 19 Sep 2022 13:52:17.958060000 UTC +00:00>

A working example of a transaction rollback


begin
  ActiveRecord::Base.transaction do
    post.update!(title: "cow")
    post.update!(content: "beer")

    raise StandardError, "cow and beer are not compatible"
  end
rescue StandardError => e
  Rails.logger.error(e.message)
end

In this case, the error affects the transaction block and stops it from being committed. Later, the error is cached by the rescue instruction and handled appropriately.

Example

Let's use a similar example. In the transaction block, we update the post twice and then raise an error. The error is rescued outside of the transaction and you can see the ROLLBACK statement in the SQL log. You can also read the final values from the post object and see they were not changed.

If you intended it to roll back the transaction - congratulations, well done!


> post = Post.create(title: 'bee', content: 'honey')

TRANSACTION (0.1ms)  BEGIN
Post Create (0.2ms)  INSERT INTO "posts" ("title", "content", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["title", "bee"], ["content", "honey"], ["created_at", "2022-09-19 13:59:45.933973"], ["updated_at", "2022-09-19 13:59:45.933973"]]
TRANSACTION (2.1ms)  COMMIT
 => #<Post:0x0000000112d4f298 id: 4, title: "bee", content: "honey", created_at: Mon, 19 Sep 2022 13:59:45.933973000 UTC +00:00, updated_at: Mon, 19 Sep 2022 13:59:45.933973000 UTC +00:00>

> begin
>   ActiveRecord::Base.transaction do
>     post.update!(title: "cow")
>     post.update!(content: "beer")
>
>     raise StandardError, "cow and beer are not compatible"
>   end
> rescue StandardError => e
>   Rails.logger.error(e.message)
> end
  TRANSACTION (0.2ms)  BEGIN
  Post Update (0.4ms)  UPDATE "posts" SET "title" = $1, "updated_at" = $2 WHERE "posts"."id" = $3  [["title", "cow"], ["updated_at", "2022-09-19 13:59:53.937241"], ["id", 4]]
  Post Update (0.2ms)  UPDATE "posts" SET "content" = $1, "updated_at" = $2 WHERE "posts"."id" = $3  [["content", "beer"], ["updated_at", "2022-09-19 13:59:53.939197"], ["id", 4]]
  TRANSACTION (0.1ms)  ROLLBACK
cow and beer are not compatible
 => 32

> post.reload
  Post Load (0.3ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT $2  [["id", 4], ["LIMIT", 1]]
 => #<Post:0x0000000112d4f298 id: 4, title: "bee", content: "honey", created_at: Mon, 19 Sep 2022 13:59:45.933973000 UTC +00:00, updated_at: Mon, 19 Sep 2022 13:59:45.933973000 UTC +00:00>

Is rescuing within the block ever correct?

Definitely. There are numerous use cases where it is the correct approach. It allows you to ignore some of the errors and let the transaction commit. You may have additional logic in such a rescue block to decide the fate of the transaction. But please, use it wisely. If you have any doubts, check the logs for TRANSACTION COMMIT or ROLLBACK entries.

Happy Coding!

Paweł Strzałkowski
Chief Technology Officer

Check my Twitter

Check my Linkedin

Did you like it? 

Sign up To VIsuality newsletter

READ ALSO

Writing Chrome Extensions Is (probably) Easier Than You Think

14
.
11
.
2023
Antoni Smoliński
Tutorial
Frontend
Backend

Bounded Context - DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial

The origin of Poltrax development - story of POLTRAX (part 2)

29
.
11
.
2023
Stanisław Zawadzki
Ruby on Rails
Startups
Business
Backend

Ruby Meetups in 2022 - Summary

14
.
11
.
2023
Michał Łęcicki
Ruby on Rails
Visuality
Conferences

Repository - DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial

Example Application - DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial

How to launch a successful startup - story of POLTRAX (part 1)

14
.
11
.
2023
Michał Piórkowski
Ruby on Rails
Startups
Business

How to use different git emails for different projects

14
.
11
.
2023
Michał Łęcicki
Backend
Tutorial

Aggregate - DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial

Visuality at wroc_love.rb 2022: It's back and it's good!

14
.
11
.
2023
Patryk Ptasiński
Ruby on Rails
Conferences
Ruby

Our journey to Event Storming

14
.
11
.
2023
Michał Łęcicki
Visuality
Event Storming

Should I use Active Record Callbacks?

14
.
11
.
2023
Mateusz Woźniczka
Ruby on Rails
Backend
Tutorial

How to rescue a transaction to roll back changes?

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Backend
Ruby
Tutorial

Safe navigation operator '&.' vs '.try' in Rails

14
.
11
.
2023
Mateusz Woźniczka
Ruby on Rails
Backend
Ruby
Tutorial

What does the ||= operator actually mean in Ruby?

14
.
11
.
2023
Mateusz Woźniczka
Ruby on Rails
Backend
Ruby
Tutorial

How to design an entity - DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial

Entity - DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial

Should I use instance variables in Rails views?

14
.
11
.
2023
Mateusz Woźniczka
Ruby on Rails
Frontend
Backend
Tutorial

Data Quality in Ruby on Rails

14
.
11
.
2023
Michał Łęcicki
Ruby on Rails
Backend
Software

We started using Event Storming. Here’s why!

14
.
11
.
2023
Mariusz Kozieł
Event Storming
Visuality

First Miłośnicy Ruby Warsaw Meetup

14
.
11
.
2023
Michał Łęcicki
Conferences
Visuality

Should I use Action Filters?

14
.
11
.
2023
Mateusz Woźniczka
Ruby on Rails
Backend
Tutorial

Value Object - DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial