無料Wikiサービス | デモページ
Linuxなどのメモ書き
検索

Adsense

MySQL トランザクション の差分
Rev.6→Rev.7  追加箇所 削除箇所


トランザクションを使うと複数のクエリをまとめてつの処理することができるとしてうことができる。処理の途中でエラーになったになって処理したいような場合はROLLBACKをすることで変更内容を元に戻(RollBack)ことができるすことができる

トランザクションはInnoDBテーブルにしか使えない使用する(デフォルトMyISAM)MyISAMではトランザクション使用できない)


InnoDBテーブルの作成

新規に作るテーブルをInnoDBにするには、以下のようにする。

mysql> CREATE TABLE friends (id SERIAL, name VARCHAR(30)
NOT NULL, address VARCHAR(100), birthday DATETIME) TYPE=InnoDB;

既存のテーブルのInnoDBに変更する場合は以下のとおり。

mysql> ALTER TABLE friends TYPE = INNODB;

トランザクションの開始完了およびROLLBACK

mysql> START TRANSACTION;
mysql> COMMIT;
mysql> ROLLBACK;


実際にやってみた時の挙動


行単位のロック

実際にやってみた時の挙動


表1 トランザクションを使った時の挙動

スレッドA
スレッドB
mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
+----+--------+
1 row in set (0.00 sec)
mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
+----+--------+
1 row in set (0.00 sec)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO friends
(name, address, birthday)
VALUES("suzuki", "Kamata xxxx",
"2006-05-13 12:34:56");
Query OK, 1 row affected (0.00 sec)

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)
mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
+----+--------+
1 row in set (0.00 sec)
mysql> COMMIT;
Query OK, 0 rows affected (0.01 sec)

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)

表1 トランザクションを使った時の挙動

スレッドA
スレッドB
mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
+----+--------+
1 row in set (0.00 sec)
mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
+----+--------+
1 row in set (0.00 sec)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO friends
(name, address, birthday)
VALUES("suzuki", "Kamata xxxx",
"2006-05-13 12:34:56");
Query OK, 1 row affected (0.00 sec)

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)
mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
+----+--------+
1 row in set (0.00 sec)
mysql> COMMIT;
Query OK, 0 rows affected (0.01 sec)

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)

表2 ROLLBACKの挙動

スレッドA
スレッドB
mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)

mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO friends
(name, address, birthday)
VALUES("tanaka", "Kamata xxxx",
"2006-05-13 12:34:56");
Query OK, 1 row affected (0.00 sec)

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
| 3 | tanaka |
+----+--------+
3 rows in set (0.00 sec)

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)
mysql> ROLLBACK;

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)
mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)

表2 ROLLBACKの挙動

スレッドA
スレッドB
mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)

mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO friends
(name, address, birthday)
VALUES("tanaka", "Kamata xxxx",
"2006-05-13 12:34:56");
Query OK, 1 row affected (0.00 sec)

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
| 3 | tanaka |
+----+--------+
3 rows in set (0.00 sec)

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)
mysql> ROLLBACK;

mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)
mysql> select id,name from friends;
+----+--------+
| id | name |
+----+--------+
| 1 | tomita |
| 2 | suzuki |
+----+--------+
2 rows in set (0.00 sec)