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
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Rails Girls Zürich Keynote
gr2m
94
13k
What's in a price? How to price your products and services
michaelherold
243
12k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
GitHub's CSS Performance
jonrohan
1030
460k
Building Applications with DynamoDB
mza
90
6.1k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
Faster Mobile Websites
deanohume
305
30k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
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 τϥϯβΫγϣϯΛுΔ࣌ɺϩοΫͷҐஔʹؾΛ͚ͭΔ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠