Skip to content

Commit 2539245

Browse files
committed
Add closed check at Cursor.iterator
Fixes #1297
1 parent d74d0bd commit 2539245

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/main/java/org/apache/ibatis/cursor/defaults/DefaultCursor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ public int getCurrentIndex() {
9494

9595
@Override
9696
public Iterator<T> iterator() {
97+
if (isClosed()) {
98+
throw new IllegalStateException("A Cursor is already closed.");
99+
}
97100
if (iteratorRetrieved) {
98101
throw new IllegalStateException("Cannot open more than one iterator on a Cursor");
99102
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,18 @@ public void shouldGetAllUserUsingAnnotationBasedMapper() {
361361
}
362362
}
363363

364+
@Test
365+
public void shouldThrowIllegalStateExceptionUsingIteratorOnSessionClosed() {
366+
Cursor<User> usersCursor;
367+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
368+
usersCursor = sqlSession.getMapper(Mapper.class).getAllUsers();
369+
}
370+
try {
371+
usersCursor.iterator();
372+
Assert.fail("Should throws the IllegalStateException when call the iterator method after session is closed.");
373+
} catch (IllegalStateException e) {
374+
Assert.assertEquals("A Cursor is already closed.", e.getMessage());
375+
}
376+
}
377+
364378
}

0 commit comments

Comments
 (0)