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
280
RailsとDBのトランザクション処理
ebisurb17 #ebisurb
https://ebisurb.connpass.com/event/93818/
ShoheiMitani
July 25, 2018
Tweet
Share
More Decks by ShoheiMitani
See All by ShoheiMitani
自己実現のためのキャリア選択 / Choosing a Career Path for Self-Realization
shoheimitani
1
150
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
3
1.2k
法律に準拠した本人確認システムを0から作った話 〜家計簿プリカB/43でのeKYC開発実例〜
shoheimitani
2
1.1k
初めましてが多いチームの形成期にEMが取り組んだ事
shoheimitani
1
200
ユーザーフレンドリーな取引明細のアーキテクチャ 〜VISAカードの複雑性に向き合う実践例〜
shoheimitani
5
2.9k
孤独のCTOグルメという やや奇抜な企画をやった目的と効果
shoheimitani
3
1.5k
VISAカードの裏側と “手が掛かる” 決済システムの育て方
shoheimitani
43
22k
32個のPRでリリースした依存度の高いコアなモデルの安全な弄り方
shoheimitani
8
7.5k
本番環境のRailsでYJITをONにしてみた結果
shoheimitani
0
190
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
4
480
Gamification - CAS2011
davidbonilla
81
5.2k
Rails Girls Zürich Keynote
gr2m
94
13k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
17
1.1k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
51
2.4k
Product Roadmaps are Hard
iamctodd
PRO
52
11k
What's in a price? How to price your products and services
michaelherold
245
12k
Building a Modern Day E-commerce SEO Strategy
aleyda
39
7.2k
Why Our Code Smells
bkeepers
PRO
336
57k
A designer walks into a library…
pauljervisheath
205
24k
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 τϥϯβΫγϣϯΛுΔ࣌ɺϩοΫͷҐஔʹؾΛ͚ͭΔ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠