云数据库 RDS-主动终止空闲事务:应用场景

时间:2023-12-13 11:32:57

应用场景

设置相关参数值如下,分别对下列参数适用的场景进行分析。
show variables like '%idle%';
+-----------------------------------+-------+
| Variable_name                     | Value |
+-----------------------------------+-------+
| idle_readonly_transaction_timeout | 5     |
| idle_transaction_timeout          | 10    |
| idle_write_transaction_timeout    | 15    |  
+-----------------------------------+-------+
  • 设置idle_readonly_transaction_timeout

    设置参数idle_readonly_transaction_timeout=5。

    1. 使用begin开启事务,执行查询语句,查询结果如下:
      begin;
      Query OK, 0 rows affected (0.00 sec)
      select * from t1;
      +-----+-----+-----+-----+
      | a   |  b  |  c  | d   | 
      +-----+-----+-----+-----+
      | 1   |  b  | 303 | d   |
      +-----+-----+-----+-----+
      1 row in set (0.00 sec)
    2. 在idle_readonly_transaction_timeout设置的5s范围以外执行一次查询操作,结果如下:
      select * from t1;
      +-----+-----+-----+-----+
      | a   |  b  |  c  | d   | 
      +-----+-----+-----+-----+
      | 1   |  b  | 303 | d   |
      +-----+-----+-----+-----+
      1 row in set (0.00 sec)
      select * from t1;
      ERROR 2006(HY000): MySQL server has gone away
  • 设置idle_transaction_timeout,idle_readonly_transaction_timeout,idle_write_transaction_timeout
    设置参数idle_transaction_timeout=10,idle_readonly_transaction_timeout=0,idle_write_transaction_timeout=0。
    show variables like '%idle%';
    +-----------------------------------+-------+
    | Variable_name                     | Value |
    +-----------------------------------+-------+
    | idle_readonly_transaction_timeout | 0     |
    | idle_transaction_timeout          | 10    |
    | idle_write_transaction_timeout    | 0     |
    +-----------------------------------+-------+
    3 rows in set (0.01 sec)
    • 只读事务

      当idle_readonly_transaction_timeout=0时,idle_transaction_timeout参数生效。

      1. 使用begin开启事务,查询表数据,结果如下:
        begin;
        Query OK, 0 rows affected (0.00 sec)
        select * from t1;
        +-----+-----+-----+-----+
        | a   |  b  |  c  | d   | 
        +-----+-----+-----+-----+
        | 1   |  b  |  43 | d   |
        +-----+-----+-----+-----+
        1 row in set (0.00 sec)
      2. 10s之后重复执行上述查询操作,结果如下:
        select * from t1;
        ERROR 2006(HY000): MySQL server has gone away
    • 读写事务

      当idle_write_transaction_timeout=0时,,idle_transaction_timeout参数生效。

      1. 使用begin开启事务后,插入数据,在10s以内执行一条查询语句,结果如下:
        begin;
        Query OK, 0 rows affected (0.00 sec)
        INSERT INTO t1(a,b,c,d) VALUES (1,'b',FLOOR( 1 + (RAND()*1000)) ,'d');
        Query OK, 1 rows affected (0.00 sec)
        select * from t1;
        +-----+-----+-----+-----+
        | a   |  b  |  c  | d   | 
        +-----+-----+-----+-----+
        | 1   |  b  |  425 | d   |
        +-----+-----+-----+-----+
        1 row in set (0.00 sec)
      2. 10s后执行一条查询语句,结果如下:
        select * from t1;
        ERROR 2006(HY000): MySQL server has gone away
      3. 之后单独执行一条查表语句,结果如下,表示此时事务已经回滚。
        select * from t1;
        Empty set (0.00 sec)
  • 设置idle_write_transaction_timeout

    设置参数idle_write_transaction_timeout=15。

    1. 首先由begin开启事务并插入一条数据,结果如下:
      begin;
      Query OK, 0 rows affected (0.00 sec)
      INSERT INTO t1(a,b,c,d) VALUES (1,'b',FLOOR( 1 + (RAND()*1000)) ,'d');
      Query OK, 1 rows affected (0.00 sec)
    2. 在idle_write_transaction_timeout设置的15s范围以内执行一次查询操作,结果如下:
      select * from t1;
      +-----+-----+-----+-----+
      | a   |  b  |  c  | d   | 
      +-----+-----+-----+-----+
      | 1   |  b  | 987 | d   |
      +-----+-----+-----+-----+
      1 row in set (0.00 sec)
    3. 15s后再次执行查询,结果如下:
      select * from t1;
      ERROR 2006(HY000): MySQL server has gone away
    4. 重新连接数据库,执行一条查询语句,结果如下,表示此时事务已经回滚。
      select * from t1;
      Empty set (0.00 sec)
support.huaweicloud.com/kerneldesc/rds_07_0000.html