Eliminate Submission

Douglas Surber douglas.surber at oracle.com
Mon Feb 4 21:28:31 UTC 2019


CancelableCompletionStage.cancel does not specify how the CancelableCompletionStage will be completed. It will be completed according to the normal rules of Operation completion. It does specify how the Operation will be completed, with SqlSkippedException if canceling succeeds. CompletableFuture.cancel(boolean) specifies that the CompletableFuture will be completed with CanceledException if the cancel succeeds. So the two methods are quite different.

If the code calling CancelableCompletionStage.cancel (just cancel in the rest of this message) cares whether the Operation is canceled it could attach a CompletionStage.

  cf.cancel();
  cf.exceptionally( t -> { if (t instanceof SqlSkippedException) System.out.println("probably canceled"); else System.out.println("not canceled");  return t; } );

Note "probably canceled" because the Operation could have been skipped due to a exception by another Operation. We could define SqlCanceledException of course. That seems ok in which case cancel returns this.

Douglas



> On Feb 4, 2019, at 1:12 PM, David Lloyd <david.lloyd at redhat.com> wrote:
> 
> Why not return CompletionStage<Void> from cancel?  In other words...
> what would it mean if it returns FALSE but there was no exceptional
> completion?
> 
> On Mon, Feb 4, 2019 at 2:57 PM Douglas Surber <douglas.surber at oracle.com> wrote:
>> 
>> I've never been happy with Submission but it seemed necessary. After thinking about it off and on for a couple of years it occurred to me that CompletionStage does not declare cancel. So I propose that ADBA define a new interface that is an extension to CompletionStage and that declares cancel.
>> 
>> Implementing this shouldn't be hard. Extend CompletableFuture and add cancel() which would just do whatever is necessary to cancel the Operation. I don't think any interaction with the CompletableFuture itself would be necessary.
>> 
>> Every use of Submission is the same or simpler with CancelableCompletionStage.
>> 
>> op.submit();                                                    no change
>> CompletionStage cs = op.submit().getCompletionStage();          remove call to getCompletionStage();
>> CompleteableFuture cf = op.submit()
>>                                .getCompletionStage()
>>                                .toCompleteableFuture();        remove call to getCompletionStage();
>> Submission s = op.submit();                                     replace Submission with CancelableCompletionStage
>> 
>> If you can see any problems with this let me know.
>> 
>> Douglas
>> 
>> package jdk.incubator.sql2;
>> 
>> /**
>> * The result of submitting an {@link Operation}. This extension to
>> * java.util.concurrent.CompletionStage adds a method {@code cancel}. Calling
>> * this method attempts to prevent or abort execution of the Operation that
>> * created this CancelableCompletionStage. This is in contrast to the {@code cancel(boolean)}
>> * method declared by java.util.concurrent.CompletableFuture which does not
>> * attempt to cancel the task that produces its result. As the signatures are
>> * different the two methods do not interfere with one another.
>> *
>> * @param <T> The type of the result of the {@link Operation} that created this
>> * {@code CancelableCompletionStage}
>> */
>> public interface CancelableCompletionStage<T> extends java.util.concurrent.CompletionStage {
>> 
>>  /**
>>   * Request that the {@link Operation} not be executed or that its execution be
>>   * aborted if already begun. This is a best effort action and may not succeed
>>   * in preventing or aborting the execution. This method does not block.
>>   *
>>   * If execution is prevented the Operation is completed exceptionally with
>>   * SkippedSqlException. If the Operation is aborted it is completed
>>   * exceptionally with SqlException.
>>   *
>>   * @return a {@link java.util.concurrent.CompletionStage} that has the value
>>   * true if the {@link Operation} is canceled.
>>   */
>>  public CompletionStage<Boolean> cancel();
>> 
>> }
>> 
> 
> 
> -- 
> - DML



More information about the jdbc-spec-discuss mailing list