Skip to content

Commit 8ac3920

Browse files
authored
Merge pull request mybatis#1494 from hutteman/cursor-close
Improve Cursor Closeable behavior
2 parents 789eeaa + b5da351 commit 8ac3920

File tree

5 files changed

+17
-24
lines changed

5 files changed

+17
-24
lines changed

src/main/java/org/apache/ibatis/cursor/Cursor.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,16 +15,14 @@
1515
*/
1616
package org.apache.ibatis.cursor;
1717

18-
import java.io.Closeable;
19-
2018
/**
2119
* Cursor contract to handle fetching items lazily using an Iterator. Cursors are a perfect fit to handle millions of
2220
* items queries that would not normally fit in memory. If you use collections in resultMaps then cursor SQL queries
2321
* must be ordered (resultOrdered="true") using the id columns of the resultMap.
2422
*
2523
* @author Guillaume Darmont / [email protected]
2624
*/
27-
public interface Cursor<T> extends Closeable, Iterable<T> {
25+
public interface Cursor<T> extends AutoCloseable, Iterable<T> {
2826

2927
/**
3028
* @return true if the cursor has started to fetch items from database.
@@ -42,4 +40,10 @@ public interface Cursor<T> extends Closeable, Iterable<T> {
4240
* @return -1 if the first cursor item has not been retrieved. The index of the current item retrieved.
4341
*/
4442
int getCurrentIndex();
43+
44+
/**
45+
* Closes the cursor.
46+
*/
47+
@Override
48+
void close();
4549
}

src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSession.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2024 the original author or authors.
2+
* Copyright 2009-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
*/
1616
package org.apache.ibatis.session.defaults;
1717

18-
import java.io.IOException;
1918
import java.sql.Connection;
2019
import java.sql.SQLException;
2120
import java.util.ArrayList;
@@ -271,11 +270,7 @@ public void close() {
271270
private void closeCursors() {
272271
if (cursorList != null && !cursorList.isEmpty()) {
273272
for (Cursor<?> cursor : cursorList) {
274-
try {
275-
cursor.close();
276-
} catch (IOException e) {
277-
throw ExceptionFactory.wrapException("Error closing cursor. Cause: " + e, e);
278-
}
273+
cursor.close();
279274
}
280275
cursorList.clear();
281276
}

src/test/java/org/apache/ibatis/binding/BindingTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import static org.junit.jupiter.api.Assertions.assertThrows;
3030
import static org.junit.jupiter.api.Assertions.assertTrue;
3131

32-
import java.io.IOException;
3332
import java.lang.reflect.Method;
3433
import java.util.ArrayList;
3534
import java.util.Collection;
@@ -63,7 +62,6 @@
6362
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
6463
import org.apache.ibatis.transaction.TransactionFactory;
6564
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
66-
import org.junit.jupiter.api.Assertions;
6765
import org.junit.jupiter.api.BeforeAll;
6866
import org.junit.jupiter.api.Test;
6967

@@ -690,8 +688,6 @@ void executeWithCursorAndRowBounds() {
690688
assertEquals(2, blog.getId());
691689
assertFalse(blogIterator.hasNext());
692690
}
693-
} catch (IOException e) {
694-
Assertions.fail(e.getMessage());
695691
}
696692
}
697693

src/test/java/org/apache/ibatis/submitted/cursor_simple/CursorSimpleTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2024 the original author or authors.
2+
* Copyright 2009-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
*/
1616
package org.apache.ibatis.submitted.cursor_simple;
1717

18-
import java.io.IOException;
1918
import java.io.Reader;
2019
import java.util.ArrayList;
2120
import java.util.Iterator;
@@ -162,7 +161,7 @@ void cursorWithRowBound() {
162161
}
163162

164163
@Test
165-
void cursorIteratorNoSuchElementExceptionWithHasNext() throws IOException {
164+
void cursorIteratorNoSuchElementExceptionWithHasNext() {
166165
try (SqlSession sqlSession = sqlSessionFactory.openSession();
167166
Cursor<User> usersCursor = sqlSession.selectCursor("getAllUsers", null, new RowBounds(1, 1))) {
168167
try {
@@ -183,7 +182,7 @@ void cursorIteratorNoSuchElementExceptionWithHasNext() throws IOException {
183182
}
184183

185184
@Test
186-
void cursorIteratorNoSuchElementExceptionNoHasNext() throws IOException {
185+
void cursorIteratorNoSuchElementExceptionNoHasNext() {
187186
try (SqlSession sqlSession = sqlSessionFactory.openSession();
188187
Cursor<User> usersCursor = sqlSession.selectCursor("getAllUsers", null, new RowBounds(1, 1))) {
189188
try {
@@ -260,7 +259,7 @@ void cursorMultipleIteratorCall() {
260259
}
261260

262261
@Test
263-
void cursorMultipleCloseCall() throws IOException {
262+
void cursorMultipleCloseCall() {
264263
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
265264
Mapper mapper = sqlSession.getMapper(Mapper.class);
266265
Cursor<User> usersCursor = mapper.getAllUsers();
@@ -290,7 +289,7 @@ void cursorMultipleCloseCall() throws IOException {
290289
}
291290

292291
@Test
293-
void cursorUsageAfterClose() throws IOException {
292+
void cursorUsageAfterClose() {
294293

295294
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
296295
Mapper mapper = sqlSession.getMapper(Mapper.class);

src/test/java/org/apache/ibatis/submitted/cursor_simple/PostgresCursorTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717

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

20-
import java.io.IOException;
2120
import java.util.Iterator;
2221

2322
import org.apache.ibatis.BaseDataTest;
@@ -53,7 +52,7 @@ static void setUp() throws Exception {
5352
}
5453

5554
@Test
56-
void shouldBeAbleToReuseStatement() throws IOException {
55+
void shouldBeAbleToReuseStatement() {
5756
// #1351
5857
try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.REUSE)) {
5958
Mapper mapper = sqlSession.getMapper(Mapper.class);

0 commit comments

Comments
 (0)