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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:26:12+00:00 2026-05-19T03:26:12+00:00

can someone explain the need to lock tables and/or rows in mysql? I am

  • 0

can someone explain the need to lock tables and/or rows in mysql?

I am assuming that it to prevent multiple writes to the same field, is this the best practise?

  • 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-19T03:26:13+00:00Added an answer on May 19, 2026 at 3:26 am

    First lets look a good document This is not a mysql related documentation, it’s about postgreSQl, but it’s one of the simplier and clear doc I’ve read on transaction. You’ll understand MySQl transaction better after reading this link http://www.postgresql.org/docs/8.4/static/mvcc.html

    When you’re running a transaction 4 rules are applied (ACID):

    • Atomicity : all or nothing (rollback)
    • Coherence : coherent before, coherent after
    • Isolation: not impacted by others?
    • Durability : commit, if it’s done, it’s really done

    In theses rules there’s only one which is problematic, it’s Isolation. using a transaction does not ensure a perfect isolation level. The previous link will explain you better what are the phantom-reads and suchs isolation problems between concurrent transactions. But to make it simple you should really use Row levels locks to prevent other transaction, running in the same time as you (and maybe comitting before you), to alter the same records. But with locks comes deadlocks…

    Then when you’ll try using nice transactions with locks you’ll need to handle deadlocks and you’ll need to handle the fact that transaction can fail and should be re-launched (simple for or while loops).

    Edit:————

    Recent versions of InnoDb provides greater levels of isolation than previous ones. I’ve done some tests and I must admit that even the phantoms reads that should happen are now difficult to reproduce.

    MySQL is on level 3 by default of the 4 levels of isolation explained in the PosgtreSQL document (where postgreSQL is in level 2 by default). This is REPEATABLE READS. That means you won’t have Dirty reads and you won’t have Non-repeatable reads. So someone modifying a row on which you made your select in your transaction will get an implicit LOCK (like if you had perform a select for update).

    Warning: If you work with an older version of MySQL like 5.0 you’re maybe in level 2, you’ll need to perform the row lock using the ‘FOR UPDATE’ words!

    We can always find some nice race conditions, working with aggregate queries it could be safer to be in the 4th level of isolation (by using LOCK IN SHARE MODE at the end of your query) if you do not want people adding rows while you’re performing some tasks. I’ve been able to reproduce one serializable level problem but I won’t explain here the complex example, really tricky race conditions.
    There is a very nice example of race conditions that even serializable level cannot fix here : http://www.postgresql.org/docs/8.4/static/transaction-iso.html#MVCC-SERIALIZABILITY

    When working with transactions the more important things are:

    • data used in your transaction must always be read INSIDE the transaction (re-read it if you had data from before the BEGIN)
    • understand why the high isolation level set implicit locks and may block some other queries ( and make them timeout)
    • try to avoid dead locks (try to lock tables in the same order) but handle them (retry a transaction aborted by MySQL)
    • try to freeze important source tables with serialization isolation level (LOCK IN SHARE MODE) when your application code assume that no insert or update should modify the dataset he’s using (if not you will not have problems but your result will have ignored the concurrent changes)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain me why would a UDP Server not need to use
Can someone please explain, in beginner terms, how operator overloading works? I need to
I don't understand the need for self-joins. Can someone please explain them to me?
Can someone explain that why do arithmetic operations on integral types in Java always
I need some help with this Boolean Implication. Can someone explain how this works
hi can someone explain how the post_param is used in SWFUpload. i need the
Can someone explain why there is the need to add an out or in
Can someone explain Breadth-first search to solve the below kind of problems I need
I don't understand why this does not work. Can someone explain what I need
I'm using Linux redhat 3, can someone explain how is that possible that i

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.