Mengenal Database Transaction

Begin, Commit, Rollback

Mengenal ACID

Atomic

Consistency

Isolation

Durability

Implementasi Database Transaction

Implementasi pada MySQL

Contoh Rollback

mysql> select * from products;+----+---------------+-------+| id | name          | stock |+----+---------------+-------+|  1 | Sampo Kelir   |    10 ||  2 | Sampo Pentin  |    20 ||  3 | Wafer Bembeng |    20 |+----+---------------+-------+3 rows in set (0.00 sec)mysql> BEGIN;Query OK, 0 rows affected (0.00 sec)mysql> update products SET stock = 9 WHERE id = 1;Query OK, 1 row affected (0.02 sec)Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from products;+----+---------------+-------+| id | name          | stock |+----+---------------+-------+|  1 | Sampo Kelir   |     9 ||  2 | Sampo Pentin  |    20 ||  3 | Wafer Bembeng |    20 |+----+---------------+-------+3 rows in set (0.00 sec)mysql> ROLLBACK;Query OK, 0 rows affected (0.01 sec)mysql> select * from products;+----+---------------+-------+| id | name          | stock |+----+---------------+-------+|  1 | Sampo Kelir   |    10 ||  2 | Sampo Pentin  |    20 ||  3 | Wafer Bembeng |    20 |+----+---------------+-------+3 rows in set (0.00 sec)

Contoh Commit

mysql> select * from products;+----+---------------+-------+| id | name          | stock |+----+---------------+-------+|  1 | Sampo Kelir   |    10 ||  2 | Sampo Pentin  |    20 ||  3 | Wafer Bembeng |    20 |+----+---------------+-------+3 rows in set (0.06 sec)mysql> BEGIN;Query OK, 0 rows affected (0.01 sec)mysql> update products SET stock = 4 WHERE id = 3;Query OK, 1 row affected (0.03 sec)Rows matched: 1  Changed: 1  Warnings: 0mysql> COMMIT;Query OK, 0 rows affected (0.01 sec)mysql> select * from products;+----+---------------+-------+| id | name          | stock |+----+---------------+-------+|  1 | Sampo Kelir   |    10 ||  2 | Sampo Pentin  |    20 ||  3 | Wafer Bembeng |     4 |+----+---------------+-------+3 rows in set (0.00 sec)

Implementasi pada PHP + MySQL

[Bonus] Implementasi pada Laravel

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store