測試MySQL交易管理

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   sp   for   on   div   

1.MySQL 版本

mysql> select version();+------------+| version()  |+------------+| 5.5.37-log |+------------+1 row in set (0.00 sec)

2.建立測試表

mysql> create table test_trans(id int ,name_ varchar(10));Query OK, 0 rows affected (0.29 sec)mysql> show table status like ‘test_trans%‘;+------------+--------+---------+------------+------+----------------+-------------+-| Name       | Engine | Version | Row_format | Rows | Avg_row_length | Data_length |+------------+--------+---------+------------+------+----------------+-------------+-| test_trans | InnoDB |      10 | Compact    |    0 |              0 |       16384 |+------------+--------+---------+------------+------+----------------+-------------+-1 row in set (0.00 sec)

3.測試事物

MySQL通過SET AUTOCOMMIT, START TRANSACTION, COMMIT和ROLLBACK等語句支援本地
事務。
文法:
START TRANSACTION | BEGIN [WORK]
COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
SET AUTOCOMMIT = {0 | 1}

預設情況下,mysql是autocommit的,如果需要通過明確的commit和rollback來提交和
復原事務,需要通過明確的事務控制命令來開始事務,這是和oracle的交易管理明顯不
同的地方。START TRANSACTION或BEGIN語句可以開始一項新的事務。
COMMIT和ROLLBACK用來提交或者復原事務。

START TRANSACTION或BEGIN語句可以開始一項新的事務。
COMMIT和ROLLBACK用來提交或者復原事務。
CHAIN和RELEASE子句分別用來定義在事務提交或者復原之後的操作,chain會立即啟動
一個新事物,並且和剛才的事務具有相同的隔離等級,release則會斷開和用戶端的串連

mysql> begin    -> insert into test_trans values(1,‘segment‘),(2,‘tablespace‘);ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘insert into test_trans values(1,‘segment‘),(2,‘tablespace‘)‘ at line 2mysql> insert into test_trans values (1,‘segment‘),(2,‘tablespace‘);Query OK, 2 rows affected (0.21 sec)Records: 2  Duplicates: 0  Warnings: 0mysql> select * from test_trans;+------+------------+| id   | name_      |+------+------------+|    1 | segment    ||    2 | tablespace |+------+------------+2 rows in set (0.00 sec)mysql> rollback;Query OK, 0 rows affected (0.00 sec)mysql> select * from test_trans;+------+------------+| id   | name_      |+------+------------+|    1 | segment    ||    2 | tablespace |+------+------------+2 rows in set (0.00 sec)a.沒有成功,報錯了,發現 begin 語句不是這樣寫的,之後再測試mysql> truncate table  test_trans;Query OK, 0 rows affected (0.06 sec)mysql> begin ;Query OK, 0 rows affected (0.00 sec)mysql> insert into test_trans values (1,‘segment‘),(2,‘tablespace‘);Query OK, 2 rows affected (0.00 sec)Records: 2  Duplicates: 0  Warnings: 0mysql> select * from test_trans;+------+------------+| id   | name_      |+------+------------+|    1 | segment    ||    2 | tablespace |+------+------------+2 rows in set (0.00 sec)mysql> rollback;Query OK, 0 rows affected (0.08 sec)mysql> select * from test_trans;Empty set (0.00 sec)mysql>b.測試下 start transactionmysql> START TRANSACTION;Query OK, 0 rows affected (0.00 sec)mysql> insert into test_trans values (1,‘segment‘),(2,‘tablespace‘);Query OK, 2 rows affected (0.00 sec)Records: 2  Duplicates: 0  Warnings: 0mysql> select * from test_trans;+------+------------+| id   | name_      |+------+------------+|    1 | segment    ||    2 | tablespace |+------+------------+2 rows in set (0.00 sec)mysql> rollback;Query OK, 0 rows affected (0.07 sec)mysql> select * from test_trans;Empty set (0.00 sec)mysql>

 

測試MySQL交易管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.