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 728101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:38:34+00:00 2026-05-14T06:38:34+00:00

I have some logic in before_save whereby (only) when some conditions are met I

  • 0

I have some logic in before_save whereby (only) when some conditions are met I let the new row be created with special_number equal to the maximum special_number in the database + 1. (If the conditions aren’t met then I do something different, so I can’t use auto-increments)

My worry is that two threads acting on this database at once might pick the same special_number if the second is executed while the first is saving. Is there way to lock the database between before_save and finishing the save, but only in some cases? I know all saves are sent in transactions, will this do the job for me?

def before_save
  if things_are_just_right
    # -- Issue some kind of lock?
    # -- self.lock? I have no idea
    # Pick new special_number
    new_special = self.class.maximum('special_number') + 1
    write_attribute('special_number',new_special)
  else
    # No need to lock in this case
    write_attribute('special_number',some_other_number)
  end
end
  • 1 1 Answer
  • 3 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-05-14T06:38:34+00:00Added an answer on May 14, 2026 at 6:38 am

    If the special number is always increasing by 1, and you cant have “gaps”, and it’s saved in the db- you can use select for update:

    SELECT special_num FROM nums FOR UPDATE;
    UPDATE nums SET special_num  = special_num  + 1;
    

    You can read about it here: select for update

    Note that it is locking the DB, so you may have a performance issue if you have lots of updates to taht special number.

    If you dont mind having gaps in the special num (in case a transaction is failed) an easy way is to use memcahced “inc” function, as it will preform very, very fast 🙂

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

Sidebar

Related Questions

I have some logic that loops and adds a new Panel control to another
I have some logic in a method that operates on a specified type and
I have some logic that depends upon two properties being set, as it executes
So here is the problem: I have some logic in my application that will
In XCode 4.2, I have some logic tests that I want to exercise against
I have some custom logic that needs to be executed every single time a
I have some simple JQuery / Javascript to perform some simple logic for all
I am creating a series of UserControls that all have some similar business logic.
i have such function to solve some logic riddle iloczyny is list with such
I have a list view and depending on some logic I want to temporary

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.