Ken Lin
    • 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
    # DB Transaction筆記 ###### tags: `Database` `Transaction` ## 目錄 [TOC] ## 認識Transaction Transaction(中文稱為交易,有些地方稱事務)是為了確保程式在處理的過程中只能全數成功才會正式寫到DB中,因此只會有兩種結果,要麼全部成功,要麼全部失敗。 這樣講好像有點模糊,舉個沒Transaction會GG的例子: - 使用網銀轉帳時,如果網銀沒有使用Transaction,轉錢的過程中突然被中斷了!那麼,當你的帳戶被扣款時,對方可能還沒有收到錢。這時你一定氣的跳腳🤬,你的錢可能已經被扣走了,但對方還沒有收到錢。 因此,資料庫系統可以在Transaction開始後,只要沒有確認成功`Commit`,就會回到交易開始前的狀態`Rollback`。 當程式中出現錯誤並需要全盤回覆原狀時,我們可以使用Transaction來保證資料正確性。 ## Transaction ACID特性 ### 原子性(Atomicity) 將整個交易啟動時到失敗或是成功時才會結束交易,因此這整段交易過程視為一個原子,是一個不可分割的邏輯單位。 ### 一致性(Consistency) 交易前後保持一致性,因此只要定義好Transaction過程重要做什麼,就要確保前後是要符合預期結果,來確保Transaction前後資料庫完整性沒有被破壞。 ### 隔離性(Isolation) Transaction過程中不能被任何別的Transaction影響。 當同一時間有兩筆資料同時進行交易,這兩筆交易的運作獨立,不會互相干擾防止不一致的結果。 ### 永久性(Durability) 一旦資料修改完成,變永久存在。 ## Transaction併發錯誤 就算前面說了這些Transaction的特性,但是在某些高併發狀況下,可能產生競爭危害(Race Condition),仍然會造成一些不可預期的錯誤 接下來都以兩次Transaction為例,T1為先開始的交易,T2為後開始的交易 ### 髒讀Dirty Read :::warning 交易(T1)讀到了交易(T2)尚未Commit的資料 ::: 可以從下圖中看到T2在更新產品數量時發生`Rollback`,因此導致T1先前讀到的數量是錯誤的 ![](https://i.imgur.com/LKzDMuN.png) ### 不可重複讀Non-repeatable read :::warning 在交易(T1)過程中,因"另一個"交易(T2)提交了Commit,當次的交易(T1)前後查詢得到不同結果 ::: 也就是在兩次查詢之間,相同的資料可能會出現不同的結果,這個狀況會導致Transaction違反前面所說的一致性 ![](https://i.imgur.com/fQMG9gF.png) ### 幻讀Phantom Read :::warning 一個交易(T1)過程中前後兩次查詢的資料總數不同,因為"另一個"交易(T2)使用INSERT或DELETE導致 ::: ![](https://i.imgur.com/z23tSFz.png) ## 資料庫隔離層級 隔離層級聽起來很複雜,可以把隔離層級當作水龍頭的開關,利用開關大小來控制水流的量,如果開的很大的話,水流得自然快(效能好),但就容易發生不可控制的狀況,反之亦然 在資料庫中就是利用隔離層級來做到解決一致性的難題,這些隔離機制是利用`鎖定`資源的概念實現的,另外實做上可能會搭配Queues的機制,不過在高併發的狀況下,Server能不能忍受這些流量也是需要考量的因素之一 ![](https://i.imgur.com/eD60H5T.png) ### Read Uncommitted 最寬鬆的隔離層級,如字面上說的,可以讀到其他Transaction尚未`Commit`的資料,可能造成前一節中所有錯誤「髒讀、不可重複讀、幻讀」 ### Read Committed 這個層級允許讀取已經`Commit`的資料,但是有可能發生「不可重複讀、幻讀」的狀況 ### Repeatable Read 在同一次Transaction的狀況下,資料庫只會讓讀取結果相同,這樣避免了不可重複讀的狀況,但仍能產生「幻讀」的結果 ### Serializable 最為嚴格的一個隔離層級,讓所有Transaction序列化執行,並且鎖定資源,確保相同時間只有一個Transaction在執行,用這個方法來避免所有發生的問題,效能最差,也最容易導致`Deadlock`問題 ### 補充: Deadlock 前面有提到這個詞就稍微說明一下,在併發狀況下Transaction互相鎖住對方的資源時就會造成Deadlock,這個時候程式就會進入死循環,直到被強制終止或是超時才會Rollback並且釋放資源 ![](https://i.imgur.com/MgKxy8n.png) ## 結論 - Transaction在高併發狀況下會產生錯誤讀取的狀況 - 在高併發的狀況下,Transaction容易產生錯誤讀取的狀況。為了避免這種狀況,可以選擇使用高隔離層級,但這樣做會對效能產生影響,因此在實際應用時,需要根據當下的情境來做取捨。 ## 參考來源 [IT鐵人賽 前端工程師一起來種一棵後端技能樹吧! Day16~19](https://ithelp.ithome.com.tw/users/20113277/ironman/2937) - Database Transaction & ACID - Transaction 併發錯誤與隔離層級 [RDBMS - 錯誤讀取現象及交易隔離層級原理介紹與舉例](https://blog.kennycoder.io/2020/02/09/RDBMS-%E9%8C%AF%E8%AA%A4%E8%AE%80%E5%8F%96%E7%8F%BE%E8%B1%A1%E5%8F%8A%E4%BA%A4%E6%98%93%E9%9A%94%E9%9B%A2%E5%B1%A4%E7%B4%9A%E5%8E%9F%E7%90%86%E4%BB%8B%E7%B4%B9%E8%88%87%E8%88%89%E4%BE%8B/)

    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 Google 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