Skip to content

Commit 4d462cb

Browse files
authored
Merge pull request #1298 from kazuki43zoo/gh-1297
Add closed check at Cursor.iterator
2 parents 8fb6440 + e83bfec commit 4d462cb

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-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
@@ -97,6 +97,9 @@ public Iterator<T> iterator() {
9797
if (iteratorRetrieved) {
9898
throw new IllegalStateException("Cannot open more than one iterator on a Cursor");
9999
}
100+
if (isClosed()) {
101+
throw new IllegalStateException("A Cursor is already closed.");
102+
}
100103
iteratorRetrieved = true;
101104
return cursorIterator;
102105
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,31 @@ 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+
// verify for checking order
378+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
379+
usersCursor = sqlSession.getMapper(Mapper.class).getAllUsers();
380+
usersCursor.iterator();
381+
}
382+
try {
383+
usersCursor.iterator();
384+
Assert.fail("Should throws the IllegalStateException when call the iterator already.");
385+
} catch (IllegalStateException e) {
386+
Assert.assertEquals("Cannot open more than one iterator on a Cursor", e.getMessage());
387+
}
388+
389+
}
390+
364391
}

0 commit comments

Comments
 (0)