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

Is Go Language the Right Choice for Your Next Project?

14
.
11
.
2023
Maciej Zdunek
Backend
Business

SXSW Tradeshow 2020: Get Your FREE Tickets and Meet Us

02
.
10
.
2024
Michał Krochecki
Ruby on Rails
Conferences
Frontend
Backend
Business

How to build effective website: simplicity & McDonald's

14
.
11
.
2023
Lukasz Jackiewicz
Ruby on Rails
Frontend
Design

Thermal Printer Protocols for Image and Text

14
.
11
.
2023
Burak Aybar
Backend
Tutorial
Software

WebUSB - Print Image and Text in Thermal Printers

14
.
11
.
2023
Burak Aybar
Backend
Tutorial
Software

What happened in Visuality in 2019

14
.
11
.
2023
Maciej Zdunek
Visuality
HR

Three strategies that work in board games and in real life

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

HR Wave - No Bullshit HR Conference 2019

14
.
11
.
2023
Alicja Gruszczyk
HR
Conferences

Lightning Talks in your company

14
.
11
.
2023
Jarosław Kowalewski
Ruby on Rails
Visuality

Stress in Project Management

02
.
10
.
2024
Wiktor De Witte
HR
Project Management

How to find good developers and keep them happy - Part 1

02
.
10
.
2024
Michał Krochecki
HR
Visuality

PKP Intercity - Redesign and case study of polish national carrier

14
.
11
.
2023
Katarzyna Szewc
Design
Business
Frontend

Let’s prepare for GITEX Dubai together!

14
.
11
.
2023
Michał Piórkowski
Conferences
Business

Ruby Quirks

14
.
11
.
2023
Jan Matusz
Ruby on Rails
Ruby

Visuality recognized as one of the Best Ruby on Rails Devs

14
.
11
.
2023
Maciej Zdunek
Ruby on Rails
Visuality
Business

Is the culture of the organization important?

14
.
11
.
2023
Alicja Gruszczyk
Conferences
Visuality

Between the devil and the deep blue sea

04
.
12
.
2023
Mateusz Wodyk
Project Management
Backend
HR

Let’s prototype!

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

5 marketing hacks which will make your life easier

14
.
11
.
2023
Maciej Zdunek
Marketing
Design