Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8616573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:39:08+00:00 2026-06-12T05:39:08+00:00

I am currently playing around with transactions and can not wrap my mind around

  • 0

I am currently playing around with transactions and can not wrap my mind around the following scenario:

Given there is user with username “johnny” and full name “John Smith”.

I start two rails consoles and perform the following commands in this order:

Console A:

ActiveRecord::Base.transaction { user = User.find_by_username("foo"); sleep 10; user.update_attribute(:full_name, "#{user.full_name}-1"); }

Console B:

ActiveRecord::Base.transaction { user = User.find_by_username("foo"); sleep 10; user.update_attribute(:full_name, "#{user.full_name}-2"); }

So the timing is the following:

A reads “John Smith”

B reads “John Smith”

A writes “John Smith-1”

B writes “John Smith-2”

According to my database class transaction B should fail to write “John Smith-2” because the data changed since it read it. So the transaction should be rollbacked and transaction A should win. I expect the username to be “John Smith-1”, but the result is “John Smith-2”.

Any ideas why this happens or how to get the expected behaviour?

Kind regards

Nils

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:39:09+00:00Added an answer on June 12, 2026 at 5:39 am

    As far as I understand transaction is not about locking, the main purpose of transaction is to ensure atomic changes. For example when you deduct money from your checking account and deposit them into your savings account you need to be sure either both INSERTs succeeded or both failed or you will be left with inconsistent state. What you need is locking, e.g. http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html.

    UPDATE: ACID is not meant to rollback transaction as I understand it. If there are no errors both transaction will succeed. What you get as a result does depend on isolation. If SERIALIZABLE level was used, you would get “John Smith-1-2”, but InnoDB by default is using REPEATABLE READ level http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html#isolevel_repeatable-read , which means that if SELECT is non-locking (User.find_by...) it will not lock record for reading and you get original result from snapshot which was created at the time transaction A started (i.e. SELECT from B will not lock until A has finished as in case of SERIALIZABLE).

    UPDATE: Meanwhile you can check http://api.rubyonrails.org/classes/ActiveRecord/Locking/Pessimistic.html for pessimistic locking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently playing around with Haskell basics and stumbled upon the following use
I'm currently playing with C, C++ and ASM. I can see that there's always
I'm currently playing around with CSS3 border-image and noticed that there are differences in
I am currently playing around with Script# (version 0.6.3) which complains about the following
I am currently playing around with LISP. Everything is fine, but I can't understand
I am currently playing around with Google's Voice Recognition API for Android SDK. What
I'm currently playing around with tipfy on Google's Appengine and just recently ran into
I'm currently playing around with some app ideas using AirPlay mirroring and a second
I'm am currently playing around with the third party facial recognition api from face.com.
I am currently playing around with chrome extensions. I would like to catch google

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.