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
270
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
68
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
1.1k
法律に準拠した本人確認システムを0から作った話 〜家計簿プリカB/43でのeKYC開発実例〜
shoheimitani
2
980
初めましてが多いチームの形成期にEMが取り組んだ事
shoheimitani
1
180
ユーザーフレンドリーな取引明細のアーキテクチャ 〜VISAカードの複雑性に向き合う実践例〜
shoheimitani
5
2.7k
孤独のCTOグルメという やや奇抜な企画をやった目的と効果
shoheimitani
3
1.4k
VISAカードの裏側と “手が掛かる” 決済システムの育て方
shoheimitani
42
21k
32個のPRでリリースした依存度の高いコアなモデルの安全な弄り方
shoheimitani
8
7.3k
本番環境のRailsでYJITをONにしてみた結果
shoheimitani
0
170
Featured
See All Featured
Building Your Own Lightsaber
phodgson
104
6.2k
A designer walks into a library…
pauljervisheath
205
24k
A Tale of Four Properties
chriscoyier
157
23k
Making the Leap to Tech Lead
cromwellryan
133
9k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.4k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
500
Statistics for Hackers
jakevdp
797
220k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
The Power of CSS Pseudo Elements
geoffreycrofte
74
5.4k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
30
2.1k
Agile that works and the tools we love
rasmusluckow
328
21k
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 τϥϯβΫγϣϯΛுΔ࣌ɺϩοΫͷҐஔʹؾΛ͚ͭΔ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠