Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
RailsとDBのトランザクション処理
Search
ShoheiMitani
July 25, 2018
0
250
RailsとDBのトランザクション処理
ebisurb17 #ebisurb
https://ebisurb.connpass.com/event/93818/
ShoheiMitani
July 25, 2018
Tweet
Share
More Decks by ShoheiMitani
See All by ShoheiMitani
法律に準拠した本人確認システムを0から作った話 〜家計簿プリカB/43でのeKYC開発実例〜
shoheimitani
2
860
初めましてが多いチームの形成期にEMが取り組んだ事
shoheimitani
1
160
ユーザーフレンドリーな取引明細のアーキテクチャ 〜VISAカードの複雑性に向き合う実践例〜
shoheimitani
5
2.5k
孤独のCTOグルメという やや奇抜な企画をやった目的と効果
shoheimitani
3
1.4k
VISAカードの裏側と “手が掛かる” 決済システムの育て方
shoheimitani
41
21k
32個のPRでリリースした依存度の高いコアなモデルの安全な弄り方
shoheimitani
8
7.1k
本番環境のRailsでYJITをONにしてみた結果
shoheimitani
0
150
7つの入金外部サービスと連携して分かった実践的な”状態管理”設計パターン3選
shoheimitani
5
5.6k
監視を通じたサービスの逐次的進化 ~B/43の決済サービスでの取り組み~ / Kaigi on Rails 2021
shoheimitani
1
2.1k
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Making Projects Easy
brettharned
115
5.9k
A Modern Web Designer's Workflow
chriscoyier
693
190k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Agile that works and the tools we love
rasmusluckow
327
21k
Thoughts on Productivity
jonyablonski
67
4.3k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
4 Signs Your Business is Dying
shpigford
180
21k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Transcript
RailsͱDBͷτϥϯβΫγϣϯॲཧ &CJTVSC
ࡾ୩ণฏ Έͨʹ͠ΐ͏͍ ͠ΐʔʔ ָఱגࣜձࣾʢݩ'BCMJDʣ &OHJOFFS ɹ!TIPIFJ
ࠓͷ w.Z42-ͷτϥϯβΫγϣϯʹ͍ͭͯ wόϦσʔγϣϯॲཧͱϩοΫͷམͱ݀͠
എܠ w ೖۚγεςϜͰΤϥʔ͕ൃੜ w .ZTRM%VQMJDBUFFOUSZbʜ`GPSLFZbdJOEFY`*/4&35ʜ w ϩοΫ੍ޚ͕ߦΘΕ͍ͯΔՕॴ w ॏෳνΣοΫߦΘΕ͍ͯΔ w
ͳͥʁʁʁ
֘ͷιʔείʔυʢΠϝʔδʣ def index … ActiveRecord::Base.transaciton do order = Order.find(params[:order_id]) item
= Item.lock.find(order.item_id) return … if payment_finished?(order, item) … Settlement.create(…) item.paid end end def payment_finished?(order, item) settlement = Settlement.find_by(order_id: order.id) settlement.present? && item.paid? end
ίʔυͷલʹ%#ͷ͓ʜ
.Z42-ͷඞਢࣝᶃɿϨϕϧ • μʔςΟϦʔυ • ೋͭͷτϥϯβΫγϣϯ͕͋ͬͨͱ͖ʹɺยํͷະίϛοτͷσʔλ͕औಘͰ͖ Δ͜ͱ • ϑΝδʔϦʔυ • τϥϯβΫγϣϯ͕։࢝ͨ͠ޙʹଞ͕ߋ৽ɺআͯ͠ίϛοτͨ͠σʔλ͕औಘͰ
͖Δ͜ͱ • ϑΝϯτϜϦʔυ • ϑΝδʔϦʔυͷInsert൛ • ϩετΞοϓσʔτ • ଞͷτϥϯβΫγϣϯ͕ίϛοτ͢ΔલͷσʔλͰɺ৽͍͠σʔλΛॻ͖͑ͯ͠ ·͏͜ͱ τϥϯβΫγϣϯʹؔ͢Δʢଞʹͬͱ͋ΔΑʜʣ
.Z42-ͷඞਢࣝᶃɿϨϕϧ Լʹߦ͘΄ͲτϥϯβΫγϣϯͰͷσʔλͷҰ؏ੑ͕ߴ͍ ৄ͘͜͜͠ಡΜͰ https://dev.mysql.com/doc/refman/5.6/ja/glossary.html#glos_isolation_level .Z42-ʹ͓͚ΔϨϕϧͱτϥϯβΫγϣϯͷ༧ঢ়گ
.Z42-ͷඞਢࣝᶄɿεφοϓγϣοτ Ͳ͏ͬͯϑΝϯτϜϦʔυ͕ղܾ͞Ε͍ͯΔͷ͔ʁʁ 14.2.4 Ұ؏ੑඇϩοΫಡΈऔΓ Ұ؏ੑಡΈऔΓͱɺInnoDB ͕ϚϧνόʔδϣϯΛ༻ͯ͠ɺ͋Δ࣌Ͱͷσʔλϕʔε ͷεφοϓγϣοτΛΫΤϦʔʹఏڙ͢Δ͜ͱΛҙຯ͠·͢ɻΫΤϦʔʹɺͦͷ࣌ΑΓ લʹίϛοτ͞ΕͨτϥϯβΫγϣϯʹΑΔมߋͷΈ͕දࣔ͞Εɺͦͷ࣌ΑΓ͋ͱͷ τϥϯβΫγϣϯ·ͨίϛοτ͞Ε͍ͯͳ͍τϥϯβΫγϣϯʹΑΔมߋදࣔ͞Ε·ͤ Μɻ
… τϥϯβΫγϣϯϨϕϧ͕ REPEATABLE READ (σϑΥϧτͷϨϕϧ) Ͱ͋Δ߹ɺ ಉ͡τϥϯβΫγϣϯͷͯ͢ͷҰ؏ੑಡΈऔΓͰɺͦͷτϥϯβΫγϣϯͷ࠷ॳͷ͜ ͷΑ͏ͳಡΈऔΓͰཱ֬͞Εͨεφοϓγϣοτ͕ಡΈऔΒΕ·͢ɻݱࡏͷτϥϯβΫγϣ ϯΛίϛοτͨ͋͠ͱʹɺ৽͍͠ΫΤϦʔΛൃߦ͢ΔͱɺΫΤϦʔͷ৽͍͠εφοϓγϣο τΛऔಘͰ͖·͢ɻ IUUQTEFWNZTRMDPNEPDSFGNBOKBJOOPECDPOTJTUFOUSFBEIUNM
.Z42-ͷඞਢࣝᶄɿεφοϓγϣοτ উखͳΠϝʔδ εφοϓγϣοτ σʔλϕʔε BIGIN SELECT ~ SELECT ~ UPDATE
~ COMMIT ⁞
վΊͯ֘ͷιʔείʔυ def index … ActiveRecord::Base.transaciton do order = Order.find(params[:order_id]) item
= Item.lock.find(order.item_id) return … if payment_finished?(order, item) … Settlement.create(…) item.paid end end def payment_finished?(order, item) settlement = Settlement.find_by(order_id: order.id) settlement.present? && item.paid? end ͍ͭͷεφοϓγϣοτʁ ͍ͭͷεφοϓγϣοτʁ
ഉଞ੍ޚͱॏෳνΣοΫ εφοϓ γϣοτ σʔλϕʔε εφοϓ γϣοτ τϥϯβΫγϣϯ" τϥϯβΫγϣϯ# 4&-&$5 4&-&$5'0361%"5&
*/4&35 61%"5& $0..*5 4&-&$5 4&-&$5'0361%"5& */4&35 61%"5& $0..*5 4&-&$5 4&-&$5 ϩοΫղ์ͪ ⁞ ➓ ߹ੑΛอͯͳ͍ͭͷ εφοϓγϣοτ͕ଘࡏ
վΊͯ֘ͷιʔείʔυ def index … ActiveRecord::Base.transaciton do order = Order.find(params[:order_id]) item
= Item.lock.find(order.item_id) return … if payment_finished?(order, item) … Settlement.create(…) item.paid end end def payment_finished?(order, item) settlement = Settlement.find_by(order_id: order.id) settlement.present? && item.paid? end ผτϥϯβΫγϣϯͷ ίϛοτΛөͰ͖ͯͳ͍
ղܾࡦ w ࣮໘ w ϩοΫॲཧτϥϯβΫγϣϯͷॳΊʹ࣮ߦ͢Δ w '0361%"5&ϩοΫղ์ޙʹ4&-&$5Λൃߦ͢Δ w "DUJWF3FDPSE#BTFUSBOTBDUJPOΑΓ "DUJWF3FDPSE-PDLJOH1FTTJNJTUJDXJUI@MPDLΛ͏
w ઃܭ໘ w ςʔϒϧΛ୯Ұʹ͢Δ w ςʔϒϧʹΞΫηε͢ΔϦΫΤετΛ੍ݶ͢Δ
·ͱΊ w .Z42-Ұ؏ੑಡΈऔΓʹΑΓϑΝϯτϜϦʔυΛࢭ͍ͯ͠Δ w ͦͷཪଆͰεφοϓγϣοτ͕࡞͞Ε͍ͯΔ w τϥϯβΫγϣϯΛுΔ࣌ɺϩοΫͷҐஔʹؾΛ͚ͭΔ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠