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

  • Home
  • SEARCH
  • 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 1019259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:59:01+00:00 2026-05-16T10:59:01+00:00

Looking for a best-practice advice: Let’s suppose I have a Account object with limit

  • 0

Looking for a best-practice advice:

Let’s suppose I have a Account object with limit attribute. Each day there can be n Payments, with sum of their amounts up to the account limit. When creating a new payment, it checks to see if it’s amount + amounts of other payments of the day are still within the account limit, and either saves the record or displays error.

Now, let’s assume I have account with limit 100$, and simultaneously two payments of 99$ are being created. Each would do a select, see that nothing is there, and proceed to save itself, resulting in 198$ total being saved.

What would you do about this? I was thinking about issuing a write lock on the payments table at the start of transaction, but that seems quite heavy-handed, seeing that I only really care about not allowing payments belonging to specific account not being read by other transactions. Are there any other options, better ways of handling this situation?

  • 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-05-16T10:59:02+00:00Added an answer on May 16, 2026 at 10:59 am

    I was thinking about issuing a write lock on the payments table at the start of transaction, but that seems quit eheavy-handed

    Assuming you’re talking about transactions already, I’m assuming you’re not using MyISAM, but instead InnoDB or some other engine that supports transactions already.

    Locking for read or write on transactions to preserve atomicity of operations is not something that should be done manually. It’s the job of your transaction’s isolation level to do that. This will also preserve atomicity of operations between the opening of the transaction and the locking of your table or records, so that no reads slip through in the meanwhile.

    For the use case you describe, what you want are transactions with the ‘SERIALIZABLE’ isolation level, which will lock for read and writes. Also, it’ll automatically lock only the records’ you’ve read from (even if you query for ranges), therefore leaving the remaining records free for manipulation.

    Through the ‘SERIALIZABLE’ isolation level, the moment you read the information from the table, any attempts to update or read those records will have to wait until that transaction is finished. Also, make sure you read before you update on the same transaction so you’re sure to be working with the correct value.

    You can read more about isolation levels here:
    http://en.wikipedia.org/wiki/Isolation_(database_systems)
    http://www.databasejournal.com/features/mysql/article.php/3393161/MySQL-Transactions-Part-II—Transaction-Isolation-Levels.htm

    You can learn how to setup the isolation level of your transactions here:
    http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html

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

Sidebar

Related Questions

I am playing with TFS 2010, and am trying to setup a build process

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.