云数据库 RDS-主动终止空闲事务:应用场景
应用场景
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。
- 使用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)
- 在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
- 使用begin开启事务,执行查询语句,查询结果如下:
- 设置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参数生效。
- 使用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)
- 10s之后重复执行上述查询操作,结果如下:
select * from t1; ERROR 2006(HY000): MySQL server has gone away
- 使用begin开启事务,查询表数据,结果如下:
- 读写事务
当idle_write_transaction_timeout=0时,,idle_transaction_timeout参数生效。
- 使用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)
- 10s后执行一条查询语句,结果如下:
select * from t1; ERROR 2006(HY000): MySQL server has gone away
- 之后单独执行一条查表语句,结果如下,表示此时事务已经回滚。
select * from t1; Empty set (0.00 sec)
- 使用begin开启事务后,插入数据,在10s以内执行一条查询语句,结果如下:
- 只读事务
- 设置idle_write_transaction_timeout
设置参数idle_write_transaction_timeout=15。
- 首先由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)
- 在idle_write_transaction_timeout设置的15s范围以内执行一次查询操作,结果如下:
select * from t1; +-----+-----+-----+-----+ | a | b | c | d | +-----+-----+-----+-----+ | 1 | b | 987 | d | +-----+-----+-----+-----+ 1 row in set (0.00 sec)
- 15s后再次执行查询,结果如下:
select * from t1; ERROR 2006(HY000): MySQL server has gone away
- 重新连接数据库,执行一条查询语句,结果如下,表示此时事务已经回滚。
select * from t1; Empty set (0.00 sec)
- 首先由begin开启事务并插入一条数据,结果如下:
- GaussDB免费数据库_GaussDB哪家好_免费高斯数据库
- GaussDB内存_云数据库GaussDB内存_高斯数据库内存-华为云
- GaussDB收费_云数据库GaussDB收费_GaussDB如何收费-华为云
- 调用函数_函数调用方式_函数工作流 FunctionGraph-华为云
- GaussDB引擎_GaussDB数据库引擎_高斯数据库引擎_华为云
- 华为云数据库 RDS for PostgreSQL 实例规格介绍
- RocketMQ的架构_RocketMQ的消息模式_分布式消息RocketMQ-华为云
- gaussdb咋样_gaussdb介绍_gaussdb的优势_gaussdb用法_gaussdb架构
- GaussDB华为部署_高斯数据库_高斯数据库华为部署_华为云
- GaussDB数据库计费_华为gaussdb_gaussdb费用_gaussdb学习