Have a look recently mysql jdbc Of cancel Source code of function , Take a note of
mysql-connector There is one in the source code CancelQueryTask(CancelQueryTaskImpl) Timing task ,CancelQueryTaskImpl Inherited TimerTask At the same time CancelQueryTask Interface .
stay ClientPreparedStatement.executeInternal The method will pass through this.startQueryTimer() Method to generate this CancelQueryTask
CancelQueryTask This object is used to execute SQL Cancelled task objects , When SQL Before execution , adopt StatementImpl.setQueryTimeout(int)（ Parameters are in seconds ） As long as the value of this parameter is not 0,
It will be there JDBC Internal and MySQL A task is created before communication ( At present connection Create a new one CancelQueryTaskImpl object ) And set the delay time (timeout Time for ),
Then it will put the task into a Timer In the task queue , Wait for the trigger to execute . If it is 0 It will return null(CancelQueryTask by null)
cancel Mission ：
run The method will create a new one SocketConnection
And then through NativeSession.sendCommand() Send a message to do cancel The operation of
send out kill query command , Also mark the status .
It'll pass later localQueryToCancel.setCancelStatus(CancelStatus.CANCELED_BY_TIMEOUT); For the current TimerTask In the task Query object , take CancelStatus Set to TIMEOUT.
This is one time because timeout And do cancel.
1. Take the initiative cancel
call StatementImpl.cancel() Method creates a new one after getting some local information SocketConnection
then adopt NativeSession.sendCommand() Send a message to do cancel The operation of send out kill query command , Also mark the status .
Back Also through localQueryToCancel.setCancelStatus(CancelStatus.CANCELED_BY_USER); For the current TimerTask In the task Query object , take CancelStatus Set to .CANCELED_BY_USER.
2. passive cancel
When performing the execSQL in the future
If CancelQueryTask Not for null,SQL Statement has not responded ,CancelQueryTask After reaching the set timeout Value will be Timer Dispatch
Will execute stopQueryTimer() Method to call CancelQueryTask.cancel(); At this time timeoutTask.cancel() It's not really about execution cancel operation ,
Just to state Set up in order to CANCELLED Just marking the State , hinder purge() Method , The discovery status is cancel , Will actually remove the task .
In from Timer The task queue for will be CancelQueryTask Mission cancel fall , And then Timer Delete all canceled tasks from the task queue of .