Oyekunle Oloyede
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # An Introduction to MySQL Locks Concurrent access to database records from different client sessions can impact data integrity and lead to the expected behavior of computer programs that perform specific actions depending on the value of the data retrieved from the database. In this article, I will give you an introduction to how MySQL locks can be used for concurrency control and guarantee data integrity while allowing multiple users or sessions to safely interact with data. I will also write about the popular types of MySQL locks. ## Prerequisite This article assumes that you have an intermediate understanding of SQL. Examples are presented, and concepts are explained using MySQL. ## What is a lock? The [wikipedia](https://en.wikipedia.org/wiki/Lock_(computer_science)) page on locks has this to say about what a lock is: > In computer science, a lock or mutex (from mutual exclusion) is a synchronization primitive: a mechanism that enforces limits on access to a resource when there are many threads of execution. A lock is designed to enforce a mutual exclusion concurrency control policy, and with a variety of possible methods, there exist multiple unique implementations for different applications. From a database standpoint, a lock is used to restrict some database data so that only one session or user can make changes to that data. ## Why you should use database locks The biggest benefit of using database locks is that when employed correctly, it guarantees data integrity while allowing concurrent access to data. Database locks exist to prevent two or more users or sessions from updating the same exact piece of data at the same exact time. You should use locks to prevent potential loss of data due to concurrent updates. ## How to use MySQL locks In this section, we will learn about how to use locks in MySQL. The two popular types of MySQL locks will also be explained. Let us begin by creating the table we will be demonstrating with. Use the statement below to create the `members` table. ```sql CREATE TABLE members ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, PRIMARY KEY (id) ); ``` We will also be connecting to the database from two sessions so we can demonstrate the behavior of each lock type. We can check the connection id of the current session as follows: ```sql SELECT CONNECTION_ID(); ``` The syntax for acquiring a table lock is presented below: ```sql -- lock a single table LOCK TABLES table_name [READ | WRITE] -- lock many tables LOCK TABLES table_name1 [READ | WRITE], table_name2 [READ | WRITE], ... ; ``` A client session that acquires a lock is solely responsible for releasing the lock. A session cannot release a lock for another session. Locks are released using the `UNLOCK` statement. ```sql UNLOCK TABLES; ``` ## Types of MySQL locks We will look at the popular types of MySQL locks and how to use them in this section of the article. The two types of MySQL locks we will learn in this article are: 1. Read (Shared) locks 2. Write (Exclusive) locks ### 1. Read (Shared) locks Read or shared locks are locks that prevent a write lock from being acquired but not other read locks. It allows multiple sessions to obtain a lock on a table for the sole purpose of reading it. Read locks have the following attributes: 1. A read lock for a table can be acquired by multiple sessions at the same time. Other sessions can also read data from the table without acquiring the lock. 2. The client session that holds a read lock can only read data from the table, but cannot write. Other sessions cannot write data to the table until the lock is released. A write operation from another session will be put into the waiting states until the read lock is released. To demonstrate, we will make some writes into the `members` table we created in the previous section, acquire a read lock on the table and perform a read. First, we will check the connection id of the session we will be performing these actions from: ```sql SELECT CONNECTION_ID(); ``` ![](https://i.imgur.com/UTGGGh8.png) Now we will proceed with the rest of the operations. ```sql INSERT INTO members(name) VALUES('Moremi Lewis'); LOCK TABLE members READ; SELECT * FROM members; ``` The result of these statements is predictable. The select statement returns the expected output: ![](https://i.imgur.com/VAcHBrP.png) Let us take it a step further by attempting to perform a write into the table. ```sql INSERT INTO members(name) VALUES('Sango Uche'); ``` This statement produces an error because a client session that holds a read lock can only read data from the table, but cannot write. ![](https://i.imgur.com/NeqglKs.png) Next, we will check the behavior of a read lock from another session. Using another session, we will attempt to perform read and write operations. We can confirm that it is indeed another session by using the `CONNECTION_ID()` MySQL function to check the current connection id. ```sql SELECT CONNECTION_ID(); ``` ![](https://i.imgur.com/j9wfFtB.png) Now let us read from the `members` table. ```sql SELECT * FROM members; ``` The read request from another session is processed. ![](https://i.imgur.com/WV4mcAL.png) A write into a table with a read lock from another session will be put in the `waiting` state until the lock is released. Let us verify this behavior by performing the following actions: Insert into the `members` table: ```sql INSERT INTO members(name) VALUES('Mallam Uba'); ``` The server waits while the lock is held. ![](https://i.imgur.com/YrZL9DE.png) We can view the state of the write using the `SHOW PROCESSLIST` statement. ![](https://i.imgur.com/Pf9ho1V.png) The state is `Waiting for table metadata lock`. From the first session, release the lock. ```sql UNLOCK TABLES; ``` Query for all members from the second session. ```sql SELECT * FROM members; ``` ![](https://i.imgur.com/wmsmbuS.png) We can see that the write is processed after the session holding the lock releases it. ### 1. Write (Exclusive) locks Write or exclusive locks are locks that prevent any other lock of any kind from being obtained from another session on the same table. Write locks have the following attributes: 1. The session that holds a write lock on a table can read and write data from the table. 2. Other sessions cannot read or write data to the table until the write lock is released. We will validate these attributes by obtaining a write lock from the first session and attempt to perform a write from the second session. From the first session, lock the `members` table. ```sql LOCK TABLE members WRITE; ``` The session with the write lock can perform a read and write as demonstrated below: ```sql INSERT INTO members(name) VALUES('Another Member'); SELECT * FROM members; ``` ![](https://i.imgur.com/GpUxmUo.png) Now from the second session, let us attempt to write into the table. ```sql INSERT INTO members(name) VALUES('second session Member'); ``` The operation goes into the `waiting state`. We can check this using `SHOW PROCESSLIST` ```sql SHOW PROCESSLIST ``` ![](https://i.imgur.com/YktKW6W.png) From the first session, release the lock and query the table, the write is done. ```sql SELECT * FROM members ``` ![](https://i.imgur.com/2mxq1AK.png) ## Conclusion Guaranteeing data integrity and providing concurrency control so that multiple processes of our application can use database data safely is a standard requirement in every well-built computer program. MySQL locks are used to ensure that our database records are not left in an inconsistent or invalid state due to the interaction of multiple sessions with it. This article demonstrated how MySQL read and write locks can be used to eliminate inaccuracies due to race conditions when multiple instances of our programs attempt to access database records at the same time. Thank you for reading. Please leave your thoughts in the comment section.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully