Control the database
The database needs a lot of control.
・Keep the data consistent.
・Prepare for system failures.
etc.
The software required for them is a DBMS (DataBase Management System).
(1)The Role of DBMS
・Define a Database.
・Use the data efficiently.
・Share a database.
・Deal with database failures.
・Respond to database confidentiality.
・Provide a SQL that can access the database
(2)The function of DBMS
I will introduce two functions of DBMS.
①Transaction management function
Database updates are managed on a transaction-by-transaction. A transaction
is a collection of several processes. For example, suppose you sell 3 canned
coffees at a convenience store. At this time,
・Record sales data for 3 canned coffees.
・Subtract 3 canned coffees from inventory management system data.
A transaction is a collection of these.
If the transaction processing ends normally, the update is confirmed. This is
called "Commit". On the other hand, returning to the state before the
transaction started is called "Rollback".
If these are not functioning properly, it happens that the sales of 3 canned
coffees are recorded, but the inventory does not decrease on the system.
The order to replenish the canned coffee will be delayed without noticing the
abnormality. These problems will occur.
②Exclusive control
Exclusive control is a function that prevents multiple users from accessing
the same data while one user is accessing it when multiple users access the
same data at the same time. If this doesn't work, the data will be inconsistent.
However, be careful of deadlocks when you use exclusive control. Deadlock
is a phenomenon in which processing waits for each other and it does not
proceed.
For example, the value of X is obtained from the DB, user A adds 50, and
user B subtracts 50 to update X on the DB.
・If exclusive control is not performed, the result is as follows.
If users A and B access at the same timing, only one of the updates will
register to the DB.
・If exclusive control is performed, the result is as follows.
X is locked while User A gets X and updates it to 200. Other users can not
use X(①〜②). When X is updated to 200, it will be unlocked and User B will
be able to use X. User B gets X and subtracts 50 and updates the value to
DB(③〜④).
[Japanese]
データベースの制御
データベースは、
・データの整合性を保つ
・障害への対応準備
など、さまざまな制御をする必要があります。そのために必要となるソフトウェアがDBMS(DataBase Management System)です。
(1)DBMSの役割
・データベースの定義
・データの効率的な利用
・データベースの共用
・データベースの障害に対する対策
・データベースの機密に対する対策
・データベースにアクセスできる言語の提供(= SQL)
(2)DBMSの機能
DBMSの機能を2つ紹介します。
①トランザクション管理機能
データベースの更新は、トランザクション単位に管理されます。トランザクションとは、いくつかの処理を1つにまとめたものです。例えば、コンビニで缶コーヒーが3つ売れたとします。このとき、
・缶コーヒー3つ分の売上データを記録
・在庫管理システムのデータから缶コーヒー3つ分を減算
をまとめたものがトランザクションになります。
トランザクション処理が正常終了すれば更新を確定します。このことをコミット処理(Commit)といいます。トランザクション開始前の状態に戻すことをロールバック処理(Rollback)といいます。
これらが正常に機能していないと、缶コーヒー3つの売上が記録されているのに、システム上では在庫が減っておらず、異常に気づかず、缶コーヒーを補充するための発注が遅れてしまうというような問題が起きてしまう可能性があります。
②排他制御機能
排他制御とは、複数の利用者が同時に同じデータにアクセスした場合、ある利用者が利用中の間は、他の利用者が使えないようにする機能です(=他を排除する)。これが機能しないと、データの矛盾が発生してしまいます。
ただし、排他制御を用いた場合、デッドロックに注意しましょう。デッドロックとは、お互いの処理が待ち状態になり、処理が進まなくなる現象のことです。
(例)DBからXの値を取得して、利用者Aは50を加算し、利用者Bは50を減算してDB上のXを更新
■排他制御しなかった場合
利用者A、Bが同じタイミングでアクセスすると、どちらか一方の更新しかDBに反映されなくなってしまいます。(Xは100か200のどちらかになる)
■排他制御した場合
排他制御を行うと、利用者AがXを取得して200に更新する間、Xはロックされます。他の利用者はXを利用できません(①〜②)。200に更新し終わるとロック解除され、利用者BがXを利用できるようになります。利用者BはXを取得して
50を減算した値をDBに更新します(③〜④)。