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
260
RailsとDBのトランザクション処理
ebisurb17 #ebisurb
https://ebisurb.connpass.com/event/93818/
ShoheiMitani
July 25, 2018
Tweet
Share
More Decks by ShoheiMitani
See All by ShoheiMitani
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
950
法律に準拠した本人確認システムを0から作った話 〜家計簿プリカB/43でのeKYC開発実例〜
shoheimitani
2
930
初めましてが多いチームの形成期にEMが取り組んだ事
shoheimitani
1
170
ユーザーフレンドリーな取引明細のアーキテクチャ 〜VISAカードの複雑性に向き合う実践例〜
shoheimitani
5
2.6k
孤独のCTOグルメという やや奇抜な企画をやった目的と効果
shoheimitani
3
1.4k
VISAカードの裏側と “手が掛かる” 決済システムの育て方
shoheimitani
41
21k
32個のPRでリリースした依存度の高いコアなモデルの安全な弄り方
shoheimitani
8
7.2k
本番環境のRailsでYJITをONにしてみた結果
shoheimitani
0
160
7つの入金外部サービスと連携して分かった実践的な”状態管理”設計パターン3選
shoheimitani
6
5.8k
Featured
See All Featured
The Cult of Friendly URLs
andyhume
78
6.1k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
KATA
mclloyd
29
14k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Mobile First: as difficult as doing things right
swwweet
222
9k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
Statistics for Hackers
jakevdp
796
220k
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 τϥϯβΫγϣϯΛுΔ࣌ɺϩοΫͷҐஔʹؾΛ͚ͭΔ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠