2019-08-02 client单方面下线,server如何感知?

锁定超时?多久超时?

已经获取锁的事务,可以最长多久才会超时?或者永不超时?这就造成永久阻塞。

试图获取锁的事务, 有个最大的等待超时, 如果在超时时间内未获取锁,则抛出异常。

MySQL server检测client连接,如果client主动推出,则回滚client事务。
如果client意外退出,且没有触发quit命令, 则MySQL Server如何感知client已经退出了呢?

lock tables

If the connection for a client session terminates, whether normally or abnormally, the server implicitly releases all table locks held by the session (transactional and nontransactional).
If the client reconnects, the locks will no longer be in effect. In addition, if the client had an active transaction, the server rolls back the transaction upon disconnect, and if reconnect occurs, the new session begins with autocommit enabled.
不管啥原因, 只要client session断开连接或者终止连接了,server都会隐式释放掉所有被这个client session持有的锁。
如果client重连, 之前client session持有的所有的锁都不在了。
如果client session开启事务后, 不管啥原因, 断开了和server的连接, 则server执行事务回滚。
如果client重连, 将开启新的session, 且没有事务。
总结下: 如果client session和server 断开了, 则server释放所有被该client session占有的锁和其他资源, 如果是事务,则回滚事务。 当client再次重连server, 则开启一个新的session,且没有事务。

begin;
... do something ...
... 网络延迟, client session断开了 server的连接
,,, 网络恢复, client继续执行剩下的sql statement, 但是此时已经是完全另一个session了, 且新session和其他非事务session一样。

因此: 如果client session事务执行, 如果断开了和server的连接,则需要开启新的事务, 并且所有的业务sql statement必须全部重新执行。

Lock wait timeout exceeded; try restarting transaction

session1 tx内获取锁超时,

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

虽然抛出获取锁超时异常, 但是tx并未退出, 且txid不变, 只有tx被commit或者rollback,事务才能退出。

You have an error in your SQL syntax; tx不退出

只有tx被commit或者rollback,事务才能退出。

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> st * from t where i=6 for update;
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 'st * from t where i=6 for update' at line 1
mysql> 

14.21.4 InnoDB Error Handling

#### 14.7.2.2 autocommit, Commit, and Rollback

InnoDB sometimes rolls back only the statement that failed, other times it rolls back the entire transaction.

一般只有deadlock才会回滚这个tx,否则仅仅回滚单个statement.

隐式提交tx

13.3.3 Statements That Cause an Implicit Commit

begin; 开启tx1
... do sometion...
begin; 开启tx2, 隐式提交tx1

如果client意外退出,且没有触发quit命令, 则MySQL Server如何感知client已经退出了呢?

8.12.5.1 How MySQL Handles Client Connections

MySQL server检测client连接,如果client主动退出,则回滚client事务。
如果client意外退出,且没有触发退出命令, 则MySQL Server如何感知client已经退出了呢?
应用GC和网络延迟导致的连接超时

https://dev.mysql.com/doc/refman/5.7/en/communication-errors.html
通信错误或者连接终止

client连接到server
  1. 先握手确立连接关系

  2. 确立了连接关系后, 因为通信错误或者连接终止, client和server断开了连接, server如何感知到client已经下线了呢?

  1. 先握手确立连接关系
    connect_timeout
    在握手阶段超时连接
    connect_timeout默认10s
  1. 确立了连接关系后, 因为通信错误或者连接终止, client和server断开了连接
    wait_timeout
    The number of seconds the server waits for activity on a noninteractive connection before closing it.
    默认8个小时
    By default, the server closes the connection after eight hours if nothing has happened.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值