AMashenkov commented on code in PR #2846:
URL: https://github.com/apache/ignite-3/pull/2846#discussion_r1423739986


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/SessionImpl.java:
##########
@@ -543,55 +549,60 @@ List<AsyncSqlCursor<?>> openedCursors() {
     }
 
     private class ScriptHandler {
+        private final CompletableFuture<Void> resFut = new 
CompletableFuture<>();
+        private final List<Throwable> cursorCloseErrors = 
Collections.synchronizedList(new ArrayList<>());
 
-        private final CompletableFuture<Void> resFut;
-
-        private ScriptHandler(CompletableFuture<Void> resFut) {
-            this.resFut = resFut;
-        }
-
-        void processFirstResult(AsyncSqlCursor<InternalSqlRow> cursor, 
Throwable t) {
-            if (t != null) {
-                resFut.completeExceptionally(t);
-            } else {
-                int cursorId = registerCursor(cursor);
-                processCursor(cursor, cursorId);
-            }
+        CompletableFuture<Void> resultFuture() {
+            return resFut;
         }
 
-        void processCursor(AsyncSqlCursor<InternalSqlRow> cursor, int 
cursorId) {
-            if (!busyLock.enterBusy()) {
-                closeCursor(cursor, cursorId);
+        void processCursor(AsyncSqlCursor<InternalSqlRow> cursor, Throwable 
scriptError) {
+            if (scriptError != null) {
+                // Stopping script execution.
+                onFail(scriptError);
 
-                resFut.completeExceptionally(sessionIsClosedException());
                 return;
             }
 
-            try {
-                if (cursor.hasNextResult()) {
-                    cursor.nextResult().whenComplete((nextCursor, t) -> {
-                        closeCursor(cursor, cursorId);
+            cursor.closeAsync().whenComplete((ignored, cursorCloseError) -> {
+                if (cursorCloseError != null) {
+                    // Just save the error for later and continue fetching 
cursors.
+                    cursorCloseErrors.add(cursorCloseError);
+                }
 
-                        if (nextCursor != null) {
-                            int nextCursorId = registerCursor(nextCursor);
-                            processCursor(nextCursor, nextCursorId);
-                        } else {
-                            resFut.completeExceptionally(t);
-                        }
-                    });
-                } else {
-                    closeCursor(cursor, cursorId);
+                if (!busyLock.enterBusy()) {
+                    onFail(sessionIsClosedException());
+                    return;
+                }
 
-                    resFut.complete(null);
+                try {
+                    if (cursor.hasNextResult()) {
+                        
cursor.nextResult().whenCompleteAsync(this::processCursor);
+                    } else {
+                        onComplete();
+                    }
+                } finally {
+                    busyLock.leaveBusy();
                 }

Review Comment:
   ```suggestion
                       if (cursor.hasNextResult()) {
                           
cursor.nextResult().whenCompleteAsync(this::processCursor);
                           return;
                       }              
                   } finally {
                       busyLock.leaveBusy();
                   }
                   
                   onComplete();
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to