前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mysql 流式查询中 中断关闭流出现假死现象

mysql 流式查询中 中断关闭流出现假死现象

作者头像
路过君
发布2023-06-10 14:48:25
3120
发布2023-06-10 14:48:25
举报

版本

mysql:mysql-connector-java:8.0.30

现象

使用流式处理,处理过程中希望中断,关闭流却无响应

原因

mysql需要将流式查询中的所有记录全部读取才能关闭流 中断时剩余的记录数量过多,遍历时间长导致假死现象

源码

com.mysql.cj.protocol.a.result.ResultsetRowsStreaming

代码语言:javascript
复制
// 关闭流
public void close() {
    Object mutex = this.owner != null && this.owner.getSyncMutex() != null ? this.owner.getSyncMutex() : this;

    boolean hadMore = false;
    int howMuchMore = 0;

    synchronized (mutex) {
        // 读取剩余的所有记录
        while (next() != null) {
            hadMore = true;
            howMuchMore++;

            if (howMuchMore % 100 == 0) {
                Thread.yield();
            }
        }

        if (!this.protocol.getPropertySet().getBooleanProperty(PropertyKey.clobberStreamingResults).getValue()
                && this.protocol.getPropertySet().getIntegerProperty(PropertyKey.netTimeoutForStreamingResults).getValue() > 0) {
            int oldValue = this.protocol.getServerSession().getServerVariable("net_write_timeout", 60);

            this.protocol.clearInputStream();

            try {
                this.protocol.sendCommand(this.commandBuilder.buildComQuery(this.protocol.getSharedSendPacket(), "SET net_write_timeout=" + oldValue,
                        this.protocol.getPropertySet().getStringProperty(PropertyKey.characterEncoding).getValue()), false, 0);
            } catch (Exception ex) {
                throw ExceptionFactory.createException(ex.getMessage(), ex, this.exceptionInterceptor);
            }
        }

        if (this.protocol.getPropertySet().getBooleanProperty(PropertyKey.useUsageAdvisor).getValue()) {
            if (hadMore) {
                this.owner.getSession().getProfilerEventHandler().processEvent(ProfilerEvent.TYPE_USAGE, this.owner.getSession(),
                        this.owner.getOwningQuery(), null, 0, new Throwable(),
                        Messages.getString("RowDataDynamic.1", new String[] { String.valueOf(howMuchMore), this.owner.getPointOfOrigin() }));
            }
        }
    }

    this.metadata = null;
    this.owner = null;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-06-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 版本
  • 现象
  • 原因
  • 源码
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档