Skip to content

Commit

Permalink
Merge pull request #1494 from hutteman/cursor-close
Browse files Browse the repository at this point in the history
Improve Cursor Closeable behavior
  • Loading branch information
harawata authored Jan 2, 2025
2 parents 789eeaa + b5da351 commit 8ac3920
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
12 changes: 8 additions & 4 deletions src/main/java/org/apache/ibatis/cursor/Cursor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,16 +15,14 @@
*/
package org.apache.ibatis.cursor;

import java.io.Closeable;

/**
* Cursor contract to handle fetching items lazily using an Iterator. Cursors are a perfect fit to handle millions of
* items queries that would not normally fit in memory. If you use collections in resultMaps then cursor SQL queries
* must be ordered (resultOrdered="true") using the id columns of the resultMap.
*
* @author Guillaume Darmont / [email protected]
*/
public interface Cursor<T> extends Closeable, Iterable<T> {
public interface Cursor<T> extends AutoCloseable, Iterable<T> {

/**
* @return true if the cursor has started to fetch items from database.
Expand All @@ -42,4 +40,10 @@ public interface Cursor<T> extends Closeable, Iterable<T> {
* @return -1 if the first cursor item has not been retrieved. The index of the current item retrieved.
*/
int getCurrentIndex();

/**
* Closes the cursor.
*/
@Override
void close();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2024 the original author or authors.
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,6 @@
*/
package org.apache.ibatis.session.defaults;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
Expand Down Expand Up @@ -271,11 +270,7 @@ public void close() {
private void closeCursors() {
if (cursorList != null && !cursorList.isEmpty()) {
for (Cursor<?> cursor : cursorList) {
try {
cursor.close();
} catch (IOException e) {
throw ExceptionFactory.wrapException("Error closing cursor. Cause: " + e, e);
}
cursor.close();
}
cursorList.clear();
}
Expand Down
4 changes: 0 additions & 4 deletions src/test/java/org/apache/ibatis/binding/BindingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -63,7 +62,6 @@
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -690,8 +688,6 @@ void executeWithCursorAndRowBounds() {
assertEquals(2, blog.getId());
assertFalse(blogIterator.hasNext());
}
} catch (IOException e) {
Assertions.fail(e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2024 the original author or authors.
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,6 @@
*/
package org.apache.ibatis.submitted.cursor_simple;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -162,7 +161,7 @@ void cursorWithRowBound() {
}

@Test
void cursorIteratorNoSuchElementExceptionWithHasNext() throws IOException {
void cursorIteratorNoSuchElementExceptionWithHasNext() {
try (SqlSession sqlSession = sqlSessionFactory.openSession();
Cursor<User> usersCursor = sqlSession.selectCursor("getAllUsers", null, new RowBounds(1, 1))) {
try {
Expand All @@ -183,7 +182,7 @@ void cursorIteratorNoSuchElementExceptionWithHasNext() throws IOException {
}

@Test
void cursorIteratorNoSuchElementExceptionNoHasNext() throws IOException {
void cursorIteratorNoSuchElementExceptionNoHasNext() {
try (SqlSession sqlSession = sqlSessionFactory.openSession();
Cursor<User> usersCursor = sqlSession.selectCursor("getAllUsers", null, new RowBounds(1, 1))) {
try {
Expand Down Expand Up @@ -260,7 +259,7 @@ void cursorMultipleIteratorCall() {
}

@Test
void cursorMultipleCloseCall() throws IOException {
void cursorMultipleCloseCall() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
Cursor<User> usersCursor = mapper.getAllUsers();
Expand Down Expand Up @@ -290,7 +289,7 @@ void cursorMultipleCloseCall() throws IOException {
}

@Test
void cursorUsageAfterClose() throws IOException {
void cursorUsageAfterClose() {

try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.util.Iterator;

import org.apache.ibatis.BaseDataTest;
Expand Down Expand Up @@ -53,7 +52,7 @@ static void setUp() throws Exception {
}

@Test
void shouldBeAbleToReuseStatement() throws IOException {
void shouldBeAbleToReuseStatement() {
// #1351
try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.REUSE)) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
Expand Down

0 comments on commit 8ac3920

Please sign in to comment.