Web制作、Web開発の歩き方

初心者のためのDjango入門

■第6話:Djangoのデータベース、トランザクション機能

(最終更新日:2023.01.07)

Djangoフレームワークのイメージ
この記事は3分で読めます!
(絵が小さい場合はスマホを横に)

「トランザクション機能とは?」

Djangoに限らずデータベースを扱っていれば、排他処理とトランザクション機能は必ず必要になる。 今回はDjangoにおけるトランザクション機能の実装方法を紹介する。


1.トランザクション機能とは

まずは、トランザクション自体について説明する。 トランザクションとは2つ以上の一連の処理が途中で終了した場合、処理を取り消す機能である。 なぜ、このような機能が必要かを以下の例で説明する。

例えば、口座の振込処理で、お婆さんから孫へ20万円振り込むとする。 その時「①お婆さんの口座から-20万円」「②孫の口座へ+20万円」という2つの処理が行われる。 この際、①の処理が終わったところで停電等が起こり、②の処理が行われなかったとする。 ②の処理が行われなかった際、①の処理を取り消すという機能がないと、お婆さんの20万円が振り込まれないまま消失することになる。 このような不具合をなくすためにトランザクション機能が必要になる。 ちなみに、処理を取り消す動作をロールバックと言う。

トランザクションが有効な例

2.トランザクション機能の3つの実装方法

次にDjangoでトランザクション機能を実装する方法を紹介する。 Djangoでトランザクション機能を実装するには、3つの方法がある。

  1. デコレーターを用いる方法(@transaction.atomic)
  2. withを使う方法(with transaction.atomic())
  3. setting.pyで設定する方法(ATOMIC_REQUESTS)

下記にそれらの例を示す。1は左上に当たる。 処理を行っているViewの関数に、@transaction.atomicを付けるだけである。 2は右上に当たる。with句でtransaction.atomic()を用いる。 3はsetting.pyのDATABASEの項目にATOMIC_REQUESTSをTrueにする。 3の場合は注意が必要で、全ての処理に対してtransaction処理が有効になってしまう。 トランザクションを適用したくないViewには、@transaction.non_atomic_requestsをデコレーターで付与する必要がある。

個人的にはtransaction処理を行っていることが明確な12がおすすめである。

トランザクションの実装方法
1.デコレーター(左上)、2.with(右上)、3.setting.py(左下)


▼参考図書、サイト

   【Django】トランザクションの設定方法  DjangoBrothers BLOG
   djangoでアトミックトランザクションを使ってみる 日々報道


Introduction to Django for Beginners ■ Episode 6: Django’s Database and Transaction Features (Last updated: 2023.01.07) Image of Django Framework You can read this in 3 minutes! (If the image is too small, rotate your smartphone horizontally) “What is the transaction feature?” Whether you're using Django or any other database system, you’ll inevitably need transaction features and concurrency control. This article introduces how to implement transaction features in Django. [Table of Contents] What is the transaction feature? Three ways to implement transactions 1. What is the transaction feature? First, let’s explain what a transaction is. A transaction is a feature that cancels a set of two or more operations if they are interrupted midway. Here’s an example of why this is necessary: Imagine a bank transfer where a grandmother sends ¥200,000 to her grandchild. The two operations are: “① Subtract ¥200,000 from the grandmother’s account” and “② Add ¥200,000 to the grandchild’s account.” If a power outage occurs after ① but before ②, and there’s no way to roll back the process, the ¥200,000 would disappear without reaching the grandchild. To prevent such issues, we need transaction features. This kind of cancel operation is called a rollback. Example of a valid transaction 2. Three ways to implement transactions Next, let’s look at how to implement transactions in Django. There are three main methods: Using a decorator (@transaction.atomic) Using a with statement (with transaction.atomic()) Enabling ATOMIC_REQUESTS in settings.py Examples of each are shown below. 1 corresponds to the top left — just add @transaction.atomic to the view function. 2 is the top right — use `with transaction.atomic()` in a block. 3 involves setting `ATOMIC_REQUESTS=True` under the DATABASES section in settings.py. Be careful with method 3, as it enables transactions on all requests. To disable it for a particular view, use the `@transaction.non_atomic_requests` decorator. Personally, I recommend method 1 or 2 because it’s more explicit. Ways to implement transactions 1. Decorator (top left), 2. With statement (top right), 3. settings.py (bottom left) ▼ Reference Books and Websites    [Django] How to Set Transaction Settings — DjangoBrothers BLOG    Using Atomic Transactions in Django — Daily Reports