In mysql streaming query, there is a false death phenomenon when the stream is interrupted and closed

Version

mysql:mysql-connector-java:8.0.30

Phenomenon

Using stream processing, I hope to be interrupted during processing, but there is no response when closing the stream

reason

Mysql needs to read all the records in the stream query to close the stream.
When the stream is interrupted, the number of remaining records is too large, and the traversal time is long, resulting in suspended animation.

source code

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

// 关闭流
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;
}

Guess you like

Origin blog.csdn.net/zhoudingding/article/details/130986420