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

WebUSB - Bridge between USB devices and web browsers

14
.
11
.
2023
Burak Aybar
Ruby on Rails
Frontend
Backend
Tutorial

Visuality comes to town - this time it's Poznań

14
.
11
.
2023
Michał Piórkowski
Visuality
HR

CSS Modules in Rails

14
.
11
.
2023
Adam Król
Ruby on Rails
Tutorial
Backend
Frontend

How to choose a software house.

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

JSON API versus the NIH syndrome

14
.
11
.
2023
Nadia Miętkiewicz
Backend
Frontend
Tutorial

From Idea to Concept

02
.
10
.
2024
Michał Krochecki
Ruby on Rails
Business
Startups

Styling React Components

14
.
11
.
2023
Umit Naimian
Ruby on Rails
Frontend
Tutorial

How good design can help your business grow

14
.
11
.
2023
Lukasz Jackiewicz
Design
Business
Marketing

TODO not. Do, or do not.

29
.
11
.
2023
Stanisław Zawadzki
Ruby on Rails
Software

CS Lessons #003: Density map in three ways

14
.
11
.
2023
Michał Młoźniak
Ruby
Backend
Tutorial
Software

Clean code for the win

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

Crowd-operated Christmas Lights

14
.
11
.
2023
Nadia Miętkiewicz
Ruby on Rails
Backend

How to startup and be mature about it

14
.
11
.
2023
Rafał Maliszewski
Ruby on Rails
Startups
Business

A journey of a thousand miles begins with workshops

14
.
11
.
2023
Michał Piórkowski
Software
HR

CS Lessons #002: Data structures

14
.
11
.
2023
Michał Młoźniak
Ruby
Software

Summary of Phoenix workshop at Visuality

14
.
11
.
2023
Karol Słuszniak
Ruby on Rails
Visuality
Backend

CS Lessons #000: Introduction and motivation

14
.
11
.
2023
Michał Młoźniak
Ruby
Software

CS Lessons #001: Working with binary files

14
.
11
.
2023
Michał Młoźniak
Ruby
Software

Working with 40-minute intervals

14
.
11
.
2023
Sakir Temel
Software
HR

THE MATURE TECH STARTUP DILEMMA: WHAT'S NEXT

14
.
11
.
2023
Susanna Romantsova
Startups

Win MVP workshop!

14
.
11
.
2023
Susanna Romantsova
Startups

FINTECH WEEK IN OSLO: WHATs & WHYs

14
.
11
.
2023
Susanna Romantsova
Conferences

MY FIRST MONTH AT VISUALITY

14
.
11
.
2023
Susanna Romantsova
Visuality
HR

NASA 1st global hackaton in Poland? Visuality Created it!

14
.
11
.
2023
Rafał Maliszewski
Ruby on Rails

Berlin StartupCamp 2016 summary

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