天天看點

mysql 檢視autocommit_MySQL的自動送出模式

預設情況下, MySQL啟用自動送出模式(變量autocommit為ON)。這意味着, 隻要你執行DML操作的語句,MySQL會立即隐式送出事務(Implicit Commit)。這個跟SQL Server基本是類似的。如果你了解SQL Server資料庫的話。

檢視autocommit模式

由于變量autocommit分會話系統變量與全局系統變量,是以查詢的時候,最好差別是會話系統變量還是全局系統變量。

mysql
           

Value的值為ON,表示autocommit開啟。OFF表示autocommit關閉。

修改autocommit模式
mysql
           

注意,上述SQL修改會話系統變量或全局系統變量,隻對目前執行個體有效,如果MySQL服務重新開機的話,這些設定就會丢失,如果要永久生效,就必須在配置檔案中修改系統變量。

[mysqld]
autocommit=0
           

不過網上還有種方式,如下所示,我在MySQL 5.6/5.7下測試,發現不生效,查了一下,這種方式似乎從MySQL 5.6開始已經不生效了,必須用autocommit=0這種方式替換。

[mysqld]
init_connect='SET autocommit=0'
           
autocommit與顯性事務的關系

對于顯性事務start transaction或begin, 在自動送出模式關閉(關閉隐式送出)的情況下,開啟一個事務上下文。首先資料庫會隐式送出之前的還未被送出的操作,同時開啟一個新事務。如有不明,可以用下面小實驗了解一下:

測試如下所示:

mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
|               1 |
+-----------------+
1 row in set (0.00 sec)
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+
1 row in set (0.00 sec)
 
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from test.testmodel_tag where name='Game';
Query OK, 1 row affected (0.00 sec)
           

此時在會話2中檢視,此時可以查詢到會話ID為1的事務資訊, 如下所示

mysql>  select connection_id();
+-----------------+
| connection_id() |
+-----------------+
|               2 |
+-----------------+
1 row in set (0.00 sec)
 
mysql> SELECT a.trx_state, 
    ->        b.event_name, 
    ->        a.trx_started, 
    ->        b.timer_wait / 1000000000000 timer_wait, 
    ->        a.trx_mysql_thread_id        blocking_trx_id, 
    ->        b.sql_text 
    -> FROM   information_schema.innodb_trx a, 
    ->        performance_schema.events_statements_current b, 
    ->        performance_schema.threads c 
    -> WHERE  a.trx_mysql_thread_id = c.processlist_id 
    ->        AND b.thread_id = c.thread_id; 
+-----------+----------------------+---------------------+------------+-----------------+--------------------------------------------------+
| trx_state | event_name           | trx_started         | timer_wait | blocking_trx_id | sql_text                                         |
+-----------+----------------------+---------------------+------------+-----------------+--------------------------------------------------+
| RUNNING   | statement/sql/delete | 2019-02-21 14:56:00 |     0.0010 |               1 | delete from test.testmodel_tag where name='Game' |
+-----------+----------------------+---------------------+------------+-----------------+--------------------------------------------------+
1 row in set (0.00 sec)
           

如果在會話1當中開啟顯性事務,那麼之前挂起的事務會自動送出,然後,你再去會話2當中查詢,就發現之前的DELETE操作已經送出。

mysql 檢視autocommit_MySQL的自動送出模式
mysql>  select connection_id();
+-----------------+
| connection_id() |
+-----------------+
|               2 |
+-----------------+
1 row in set (0.00 sec)
 
mysql> SELECT a.trx_state, 
    ->        b.event_name, 
    ->        a.trx_started, 
    ->        b.timer_wait / 1000000000000 timer_wait, 
    ->        a.trx_mysql_thread_id        blocking_trx_id, 
    ->        b.sql_text 
    -> FROM   information_schema.innodb_trx a, 
    ->        performance_schema.events_statements_current b, 
    ->        performance_schema.threads c 
    -> WHERE  a.trx_mysql_thread_id = c.processlist_id 
    ->        AND b.thread_id = c.thread_id; 
+-----------+----------------------+---------------------+------------+-----------------+--------------------------------------------------+
| trx_state | event_name           | trx_started         | timer_wait | blocking_trx_id | sql_text                                         |
+-----------+----------------------+---------------------+------------+-----------------+--------------------------------------------------+
| RUNNING   | statement/sql/delete | 2019-02-21 14:56:00 |     0.0010 |               1 | delete from test.testmodel_tag where name='Game' |
+-----------+----------------------+---------------------+------------+-----------------+--------------------------------------------------+
1 row in set (0.00 sec)
 
mysql> SELECT a.trx_state, 
    ->        b.event_name, 
    ->        a.trx_started, 
    ->        b.timer_wait / 1000000000000 timer_wait, 
    ->        a.trx_mysql_thread_id        blocking_trx_id, 
    ->        b.sql_text 
    -> FROM   information_schema.innodb_trx a, 
    ->        performance_schema.events_statements_current b, 
    ->        performance_schema.threads c 
    -> WHERE  a.trx_mysql_thread_id = c.processlist_id 
    ->        AND b.thread_id = c.thread_id; 
Empty set (0.00 sec)
           

With START TRANSACTION, autocommit remains disabled until you end the transaction with COMMIT or ROLLBACK. The autocommit mode then reverts to its previous state

使用START TRANSACTION,自動送出将保持禁用狀态,直到你使用COMMIT或ROLLBACK結束事務。 自動送出模式然後恢複到之前的狀态(如果start transaction 前 autocommit = 1,則完成本次事務後 autocommit 還是 1。如果 start transaction 前 autocommit = 0,則完成本次事務後 autocommit 還是 0)

(完)